2017-07-30 66 views
0

我正在製作一個生成QR碼並將其保存到相機膠捲的應用程序,但調用保存功能會導致應用程序崩潰,並帶有致命錯誤:意外地發現零展開可選值。將應用程序生成的qrcode保存到相機膠捲(Swift 3)

 @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) 


} 

的錯誤出現在第2行,關於
compressedImage = UIImage的(數據:爲imageData)。

+1

的可能的複製[什麼是「致命的錯誤:意外發現零而展開的可選值」?意思是(https://stackoverflow.com/questions/32170456 /什麼,確實致命錯誤 - 意外發現的,未繳而-去包裹-AN-可選-VALU) – Keiwan

回答

0

這是我如何解決我的問題:

 @IBAction func saveTapped(_ sender: Any) { 
    UIGraphicsBeginImageContext(codeView.frame.size) 
    codeView.layer.render(in: UIGraphicsGetCurrentContext()!) 
    let output = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 

     UIImageWriteToSavedPhotosAlbum(output!, nil, 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) 
} 
相關問題