In my project, I am capturing an image. But when it starts saving the image, app crashes with the error of -
"unexpectedly found nil while unwrapping"
My code is as follows-
// Library Assets
var assetCollection: PHAssetCollection!
var photoAsset: PHFetchResult!
let saveImage = UIImage(CGImage: cgImage, scale: 1, orientation: UIImageOrientation.Down)
if let actualSaveImage = saveImage {
// Save image
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
let request = PHAssetChangeRequest.creationRequestForAssetFromImage(actualSaveImage)
let assetPlaceholder = request.placeholderForCreatedAsset
let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: self.assetCollection, assets: self.photoAsset)
albumChangeRequest.addAssets([assetPlaceholder])
self.buttonCapture.hidden = false
self.activityIndicator.stopAnimating()
self.lastShotFlag = false
}, completionHandler: {(success, error)in
NSLog("\nSave Image -> %@", (success ? "Success" : "Error!"))
})
}
else {
NSLog("\nProblem in saveImage. It's nil.")
}
After debugging it looks like I am not getting any value for the constant albumChangeRequest