swift - UIImagePickerController 的 xcode 8.1 和 iOS 10.1.1 "[Generic] Creating an image format with an unknown type is an error"

标签 swift xcode uiimagepickercontroller

尝试允许用户从照片库中选择照片,然后在 UIImageView 中显示该图像。照片库显示得很好,但是当我从库中选择照片时,出现此错误“[通用]创建未知类型的图像格式是错误”。

单步执行下面的代码,但仅当选择不在这两个函数中出现的图像时才会出现错误。

@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但它们都不起作用,而且听起来有几个人没有解决这个问题。有任何想法吗?

最佳答案

您是否尝试在选择器之前添加_?我相信该方法已更改,并且您正在使用的方法已被弃用,您应该能够让它自动完成。

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

关于swift - UIImagePickerController 的 xcode 8.1 和 iOS 10.1.1 "[Generic] Creating an image format with an unknown type is an error",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41006724/

相关文章:

ios - 实例化 AVCaptureDeviceInput 时出错

objective-c - (Mac) 创建执行 shell 脚本的 Xcode 应用程序

ios - 代码 8 : Symbol(s) not found for architecture arm64 for pods

ios - 删除 Collection View 中的多个图像

swift - 字符串到 C 字符** Swift 3

swift - 使用 "open"和 "public"

ios - 如何在 Xcode 中删除空白并将背景扩展到全屏?

ios - 如何在将图像加载到 View Controller 之前裁剪图像

ios - 如何检测 UIImagePickerController 预览状态?

swift - 使用 uiTextView 关闭键盘