Thought I had a good lead down in ImageIO, but got stuck there too:
let docsurl = try! FileManager.default.url(for:.documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let url = docsurl.appendingPathComponent("output.heic")
// tried setting number of images to 2 here V
// also tried setting the number of images to 1 and just add two down below.
guard let destination = CGImageDestinationCreateWithURL(url as CFURL, AVFileType.heic as CFString, 2, nil) else {
fatalError("unable to create CGImageDestination")
// this throws the exception "findWriterForType:148: capacity parameter (2) is too large for this file format (max is 1)"
}
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
CGImageDestinationAddImage(destination, image.cgImage!, nil)
CGImageDestinationAddImage(destination, image.cgImage!, nil) // in second attempt, this fails saying there are not enough images
// never get here
if (CGImageDestinationFinalize(destination)) {
PHPhotoLibrary.shared().performChanges({
let create : PHAssetCreationRequest = PHAssetCreationRequest.forAsset()
create.addResource(with:.photo , fileURL: url, options: nil)
}, completionHandler: { (success, error) in
print(success, error ?? "no error")
})
}