Saving Already Created Live Photos

2019-04-12 21:43发布

问题:

I've got some live photos created JPEG and MOV files, now i want to import them into the app that would allow the user to save the live photos to their photo library. How can i go about doing this?

I've looked into this: https://github.com/genadyo/LivePhotoDemoSwift Which basically allows you to record video and turn it into a live photo. But since i've already created the "live photos", can i save them to the camera roll right away or do i need to follow a different route?

回答1:

You can create a LivePhoto from separate elements from a LivePhoto by using PHLivePhoto.requestLivePhotoWithResourceFileURLs, you will then be able to save it to the library.

func makeLivePhotoFromItems(imageURL: NSURL, videoURL: NSURL, previewImage: UIImage, completion: (livePhoto: PHLivePhoto) -> Void) {
    PHLivePhoto.requestLivePhotoWithResourceFileURLs([imageURL, videoURL], placeholderImage: previewImage, targetSize: CGSizeZero, contentMode: PHImageContentMode.AspectFit) {
        (livePhoto, infoDict) -> Void in
        if let lp = livePhoto {
            completion(livePhoto: lp)
        }
    }
}

makeLivePhotoFromItems(imgURL, videoURL: movURL, previewImage: prevImg) { (livePhoto) -> Void in
    // "livePhoto" is your PHLivePhoto object, save it/use it here
}

You will need the JPEG file URL, the MOV file URL, and a "preview" image (which is usually just the JPEG or a lighter version of it).

Full example working in a Playground here.