Live Photo From NSData
Asked Answered
V

0

7

I store live photos on the server as they come, with two separate files a JPG, and MOV file. When i retrieve them i am left with NSData, which is where my issue arises, i don't know how to create a PHLivePhoto out of that data.

Here is how i create an PHLivePhoto if the files are bundled up in my project:

let imgURL = NSBundle.mainBundle().URLForResource("IMG_0001", withExtension: "JPG")!
let movURL = NSBundle.mainBundle().URLForResource("IMG_0001", withExtension: "mov")!

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
        // for debugging: print(infoDict)
        if let lp = livePhoto {
            completion(livePhoto: lp)
        }
    }
}

 //  Credit :
   https://mcmap.net/q/638598/-working-with-live-photos-in-playground

This gives me a PHLivePhoto. What approach should i take to display the photos to the user?

Viyella answered 23/2, 2016 at 16:56 Comment(4)
Maybe by storing the NSData (converting it into jpg/mov if needed) and use the same method?Wooley
Would converting them require saving them to disk?(Which i dont want, at least not permanently), since the function requires i provide the path to the file.Viyella
You can save them into a temporary folder that you delete after while. But it seems that PHLivePhoto need two URLs, so that seems the only way to do it.Wooley
Seems like it. I'll give it a try, thanks.Viyella

© 2022 - 2024 — McMap. All rights reserved.