2016-09-18 180 views
0

採摘像我跟隨此Apple Tutorial,我得到這個錯誤:從照片庫

Creating an image format with an unknown type is an error

,同時從圖片庫中選擇圖像。

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
    let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage 
    photoImageView.image = selectedImage 
    dismiss(animated: true, completion: nil) 
} 
+0

的可能的複製[XCODE 8 - 創建具有未知類型的圖像格式是錯誤的(http://stackoverflow.com/questions/39009889/xcode-8 -create-an-image-format-with-an-unknown-type-an-an-error) –

回答

0

試試這個......我是這樣做的。

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 

    @IBOutlet weak var photoImageView: UIImageView! 
    var standardImg: UIImage! 
    var imagePicker: UIImagePickerController! 

    override func viewDidLoad() { 
     super.viewDidLoad() 


    } 

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
     if let image = info[UIImagePickerControllerOriginalImage] as? UIImage { 
      standardImg = image 
      photoImageView.image = standardImg 
     } 
      imagePicker.dismiss(animated: true, completion: nil) 
    } 

    @IBAction func addImage(_ sender: UIButton) { 

     imagePicker = UIImagePickerController() 
     imagePicker.delegate = self 
     present(imagePicker, animated: true, completion: nil) 
//  sender.setImage(standardImg, for: UIControlState()) 

    } 
} 
+0

我把'selectedImage'改成了'image',我得到了同樣的錯誤。 –

+0

你是否也改變了功能?如果你看到我的功能參數,他們是不同的,那麼你.. –

+0

也創建一個var imagePicker:UIImagePickerController!並說imagePicker.dismiss –

0

解是

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) 
{ 

    if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage { 
     photoImageView.image = selectedImage 
    } 
    else{ 
     print("Picking image is not in correct format") 
    } 

    self.dismiss(animated: true, completion: nil) 
}