Create a photo album in one asset/HEIF Collection

I'm looking to create a small photo album (max 10 pics) in a single asset as a HEIF container. From the WWDC presentations this is possible, I just don't see any direct way of doing it. Any ideas?


This does not work:

PHPhotoLibrary.shared().performChanges({

let create : PHAssetCreationRequest = PHAssetCreationRequest.forAsset()

let data : Data = UIImagePNGRepresentation(UIImage(named:"pic1")!)!

create.addResource(with: .photo, data: data, options: nil) // pic1

create.addResource(with: .photo, data: data, options: nil) // pic2

)}


Maybe through an AVAssetWriter?


Thanks in advance,

Dan

Replies

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")

})

}

I'm wondering if you ever got an answer to ths question.


I'm trying to do the same thing. I've been able to get it work when the image capacity in CGImageDestinationCreateWithURL() is set to 1, but when I set it 2, I get the same error that the max capacity for the file format is 1.

The WWDC sessions about HEIC explicilty mentioned the HEIF container's capability of storing image sequences and albums, and that being one of their motivations to move over to HEIF. But I've scoured through the documentation and talks and the internet and there doesn't seem to be an answer on how to add more than one image to a HEIC file.

Seems like macOS High Sierra doesn't support creating HEIC containers larger than 1 image. Same call on macOS Mojave is without problems

If you run this code in iOS 13, it works (you can add more than one image), but I'm not certain what happens internally to the image. Still working through how it might be useful to me/making an album.

I've been able to add multiple images to a HEIC container using CGImageDestinationCreateWithURL( ). Adding two identical images yielded a file size exactly double of a HEIC file with only one image added to the container.

So this doesn't seem to be the method to use if you're looking to encode an image sequence that employs HEVC.