4 Replies
      Latest reply on Jul 19, 2019 12:38 PM by danloughney
      danloughney Level 1 Level 1 (0 points)

        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

        • Re: Create a photo album in one asset/HEIF Collection
          danloughney Level 1 Level 1 (0 points)

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

                      })

                  }