2

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?

Sam Bing
  • 2,794
  • 1
  • 19
  • 29
  • 1
    You could use `PHLivePhoto.requestLivePhotoWithResourceFileURLs` to create a Live Photo from the JPEG and the MOV - then you would be able to save it to the library. Take example in my answer here: http://stackoverflow.com/questions/33990830/working-with-live-photos-in-playground – Eric Aya Feb 23 '16 at 09:10
  • Super useful comment Eric. I would say that solves it, add an answer if you wish and i'll accept it. Thanks. – Sam Bing Feb 23 '16 at 09:15

1 Answers1

4

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.

Community
  • 1
  • 1
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • Note: the full [example](http://stackoverflow.com/questions/33990830/working-with-live-photos-in-playground) also has a Swift 3 version. – Eric Aya Jun 14 '16 at 21:51
  • 2
    Could you please explain how to save a `PHLivePhoto` instance to photo library? – liuyaodong Jul 07 '16 at 12:33