I want to create live photo with 2 URLs and 1 UIImage.
Image and video in Firebase Storage.
When i use files localy, I can create. But if i download this, i have this error Invalid image metadata. Check my code, please.
func observeLivePhotos() {
let photosRef = Database.database().reference().child("Images/" + (selectedCategory?.nameOfCategory)!)
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)
print(tempPhotos)
//Video
let urlData = NSData(contentsOf: URLvideoURL)
let urlPlaceholder = NSData(contentsOf: placeholderURL)
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let video = documentsURL.appendingPathComponent("Video.mov")
let img = documentsURL.appendingPathComponent("image.JPG")
urlData?.write(to: video, atomically: true)
self.videoURL = video
URLSession.shared.dataTask(with: placeholderURL, completionHandler: { (data, response, error) in
if error != nil {
print(error)
return
}
self.placeholderImage = UIImage(data: data!)!
})
URLSession.shared.dataTask(with: URLimageURL, completionHandler: { (data, response, error) in
if error != nil {
print("error on mainImage")
return
}
self.mainImage = UIImage(data: data!)!
})
do {
try UIImageJPEGRepresentation(self.mainImage, 1.0)?.write(to: img, options: .atomic)
} catch { }
self.mainImageURL = img
self.makeLivePhotoFromItems(completion: { (live) in
self.liveView.livePhoto = live
})
}
}
self.livePhotos = tempPhotos
self.liveView.reloadInputViews()
}
}