Export LivePhoto via UIActivityViewController - Swift

206 Views Asked by At

I was able to successfully create a LivePhoto having a jpeg and mov files and also to display it on screen.

Now I'm having trouble for save the LivePhoto to library. With the below code I'm receiving the error: "resource not available".

 @IBAction func exportButton(_ sender: Any) {

        PHLivePhoto.request(withResourceFileURLs: [imgUrl!,videoUrl!], placeholderImage: previewImg, targetSize: CGSize.zero, contentMode: PHImageContentMode.aspectFit, resultHandler: { (livePhoto,info) -> Void in

            let items = [livePhoto] as [Any]
            let ac = UIActivityViewController(activityItems: items as [Any], applicationActivities: nil)
            self.present(ac, animated: true)
        })

    }

Thank you in advance!

1

There are 1 best solutions below

0
On

I was able to export the image correctly using this piece of code:

PHPhotoLibrary.shared().performChanges({
                let creationRequest = PHAssetCreationRequest.forAsset()
                let options = PHAssetResourceCreationOptions()
                creationRequest.addResource(with: PHAssetResourceType.pairedVideo, fileURL: videoUrl!, options: options)
                creationRequest.addResource(with: PHAssetResourceType.photo, fileURL: imgUrl!, options: options)
            }, completionHandler: { (success, error) in
                if error != nil {
                    print(error)
                }
                print(success)
            })