2016-12-06 11 views
0

試圖允許用戶從他們的照片庫中選擇一張照片,然後在UIImageView中顯示該圖像。照片庫顯示得很好,但是當我從我的庫中選擇照片時,出現此錯誤「[通用]使用未知類型創建圖像格式是錯誤」。xcode 8.1&iOS 10.1.1「[通用]創建一個未知類型的圖像格式是錯誤的」對於UIImagePickerController

通過我的代碼在下面跳過,但只有當選擇一個圖像不出現在這兩個函數中的任何一個時,錯誤纔會出現。

@IBAction func openPhotoLibraryButton(sender: UIButton) { 
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) { 
     let imagePicker = UIImagePickerController() 
     imagePicker.delegate = self 
     imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary; 
     imagePicker.allowsEditing = false 
     self.present(imagePicker, animated: true, completion: nil) 
    } 
} 

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {   
    let image = info[UIImagePickerControllerOriginalImage] as! UIImage! 
    let imageData = UIImageJPEGRepresentation(image!, 0.6) 
    let compressedJPGImage = UIImage(data: imageData!) 
    imagePicked.image = compressedJPGImage 
} 

試過這裏提出的解決方案:xCode 8 - Creating an image format with an unknown type is an error但他們沒有工作,這聽起來像一些人沒有得到這個問題解決了。有任何想法嗎?

回答

0

是否嘗試在拾取器之前添加_?我相信該方法已更改,並且您正在使用的方法已被棄用,您應該可以讓其自動完成。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage { 
    if let imageData = UIImageJPEGRepresentation(image, 0.6) { 
     let compressedJPGImage = UIImage(data: imageData) 
     imagePicked.image = compressedJPGImage 
    } 
    } else { 
    print("Image Picker Failure") 
    //handle any possible image picker issues/failures so that app doesn't crash in the future 
    //troubleshoot further if it always hits this 
    } 
    dismissViewControllerAnimated(true, completion: nil) 
} 
+0

是的,我用_之前選擇器:並得到相同的錯誤。該錯誤發生在方法的第一行之前,因爲它出現在我在imagePickerController()的開始處添加的打印語句之前。此外,它現在更糟糕,現在有一個新的致命錯誤:2016-12-06 16:45:53.105207 CameraDemo [714:226333] [通用]創建圖像格式與未知類型是一個錯誤 圖像選擇器func 致命錯誤:意外地發現零,而解包一個可選值 2016-12-06 16:45:53.330483 CameraDemo [714:226333]致命錯誤:意外地發現零,而解包一個可選值 (lldb) – user2512813

+0

不知道它是多麼有意義設置一個方法簽名的斷點,但我做了,它並沒有停在那裏。它停在方法的第一行,這似乎是罪魁禍首:let image = info [UIImagePickerControllerOriginalImage] as! UIImage的! – user2512813

相關問題