I'm trying to get the thumbnail of the video I have selected.
It seems like it does create the thumbnail but it crashes when uploading it to firebase
It crashes at thumbnailStorageRef
I believe.
Here's the relevant code :
guard let imagePickerUrl = info[UIImagePickerControllerMediaURL] as? URL else { return }
let videoUrl = imagePickerUrl
// Generate image thumbnail.
let asset: AVAsset = AVAsset(url: videoUrl as URL)
let imageGenerator = AVAssetImageGenerator(asset: asset)
imageGenerator.appliesPreferredTrackTransform = true
var time = asset.duration
time.value = min(time.value, 3)
do {
let thumbnailImage = try imageGenerator.copyCGImage(at: time , actualTime: nil)
let image = UIImage(cgImage: thumbnailImage)
let imageData = UIImagePNGRepresentation(image)!
let thumbnailStorageRef = FIRStorage.storage().reference()
thumbnailStorageRef.child("thumbnails/" + randomString(length: 20) + ".png")
thumbnailStorageRef.put(imageData, metadata: nil, completion: { (thumbnailMeta, error) in
if error != nil {
print("An error has occured while uploading thumbnail:",error ?? "")
} else {
print("Thumbnail upload to database was successfull", thumbnailMeta?.downloadURL() ?? "")
}
})
} catch {
print("An error has occurred while making thumbnail:")
}
When crashing the error console says:
2017-08-11 17:19:08.656 Koala[17914:491998] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[1]' *** First throw call stack:
I don't know what this error message is trying to say, maybe that the image trying to upload is nil? I'm not sure.
It looks like you forgot to use the child storage node you just created. As such, try replacing this code:
thumbnailStorageRef.child("thumbnails/" + randomString(length: 20) + ".png")
thumbnailStorageRef.put(imageData, metadata: nil, completion: ...
with this instead:
let imageRef = thumbnailStorageRef.child("thumbnails/" + randomString(length: 20) + ".png")
imageRef.put(imageData, metadata: nil, completion: ...
This change should fix your current issue ;)