How to write jpg, mov and display it on live photo?

I'm using Firebase (backend) for downloading data (videos and images).

I try to create a live photo.



This is my function for create live photo:



private func makeLivePhotoFromItems(completion: @escaping (PHLivePhoto) -> Void) {

PHLivePhoto.request(withResourceFileURLs: [imageURL, videoURL], placeholderImage: placeHolder, targetSize: CGSize.zero, contentMode: .aspectFit) {

(livePhoto, infoDict) -> Void in

if let canceled = infoDict[PHLivePhotoInfoCancelledKey] as? NSNumber,

canceled == 0,

let livePhoto = livePhoto

{

completion(livePhoto)

}

}

}



`imageURL`, `videoURL` are local URL. Placeholder image is `UIImage`



And my firebase request.





func observeLivePhotos() {

let photosRef = Database.database().reference().child("Images/")

photosRef.observe(.value) { (snapshot) in

var tempPhotos = [LivePhotos]()

for child in snapshot.children {

if let childSnapshot = child as? DataSnapshot,

let dict = childSnapshot.value as? [String: Any],

let imageURL = dict["imageURL"] as? String,

let placeholder = dict["placeholder"] as? String,

let videoURL = dict["videoURL"] as? String,

let URLimageURL = URL(string: imageURL),

let placeholderURL = URL(string: placeholder),

let URLvideoURL = URL(string: videoURL) {

let livePhoto = LivePhotos(imageURL: URLimageURL, placeholder: placeholderURL, videoURL: URLvideoURL)

tempPhotos.append(livePhoto)

//DispatchQueue.global(qos: .background).async

if let url = URL(string: videoURL),

let urlVideo = NSData(contentsOf: url),

let imageURL = NSData(contentsOf: URLimageURL) {

ImageService.getImage(withURL: placeholderURL, completion: { (image) in

self.placeHolder = image!

})

ImageService.getImage(withURL: URLimageURL, completion: { (image) in

self.imageToURL = image!

})

let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!

let fileURL = documentsURL.appendingPathComponent("image.JPG")

let videoURLL = documentsURL.appendingPathComponent("video.mov")

self.imageURL = fileURL

self.videoURL = videoURLL

do {

if let jpgImageData = UIImageJPEGRepresentation(self.imageToURL, 1.0) {

try! jpgImageData.write(to: fileURL, options: .atomic)

urlVideo.write(to: videoURLL, atomically: true)

}

}

}

}

}

self.makeLivePhotoFromItems(completion: { (photo) in

self.liveView.livePhoto = photo

})

self.livePhotos = tempPhotos

self.liveView.reloadInputViews()

}

}



The request to database with `[String: Any]` is correct. If I `print(tempPhotos)` , I can see my URLs from Firebase Database.



Next step is local write. I want to download images and videos form URL (videoURL, imageURL and placeholder). Then, I want to write in local directory of application and save local URL to my images and video. But I get this error, when I call `makeLivePhotoFromItems` -



Error: file doesn't exist: file:///Users/user/Library/Developer/CoreSimulator/Devices/bla-bla/data/Containers/Data/Application/bla-bla/Documents/video.mov

Replies

Hi there:


I was in the same situation and found this:

https://github.com/aasatt/LivePhotoDuplicator/tree/master/LivePhotoDuplicator


There you have both quicktime and jpeg helpers to write the file. But anyway followed another path: Create both files locally and upload it to firebase instead, so I remove the client side conversion.


Regards