I am making an app which generates QR-code and saves it to the camera roll, but calling the saving function causes app crash with a fatal error: unexpectedly found nil while unwrapping an Optional value.
@IBAction func saveTapped(_ sender: Any) {
let imageData = UIImagePNGRepresentation(codeView.image!)
let compressedImage = UIImage(data: imageData!)
UIImageWriteToSavedPhotosAlbum(compressedImage!, self, nil, nil)
let alert = UIAlertController(title: "Image Saved", message: "Your QR code is saved to CameraRoll", preferredStyle: .alert)
let okayAction = UIAlertAction(title: "Okay", style: .default, handler: nil)
alert.addAction(okayAction)
self.present(alert, animated: true, completion: nil)
}
The error comes on line 2, on
compressedImage = UIImage(data: imageData!).