ios 使用 UIImagePickerController “创建未知类型的图像格式是错误的”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39009889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:56:46  来源:igfitidea点击:

"Creating an image format with an unknown type is an error" with UIImagePickerController

iosswiftxcodeuiimageviewxcode8

提问by Jeet

While choosing an image from the image picker in iOS 10 Swift 3 I am getting an error - Creating an image format with an unknown type is an error

在 iOS 10 Swift 3 中从图像选择器中选择图像时出现错误 - Creating an image format with an unknown type is an error

 func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {

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

The image is not getting selected and updated. I need help or suggestion to know if the syntax or anything regarding this method has been changed in iOS10 or Swift 3 or is there any other way to do this.

图像未被选中和更新。我需要帮助或建议来了解 iOS10 或 Swift 3 中有关此方法的语法或任何内容是否已更改,或者是否有任何其他方法可以执行此操作。

采纳答案by Jeet

Below mentioned code did solve the problem for me -

下面提到的代码确实为我解决了问题-

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        imagePost.image = image
    } else{
        print("Something went wrong")
    }

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

回答by Roberto Gutierrez

Remember to add delegate to self

记得给自己添加委托

let picker = UIImagePickerController()
picker.delegate = self // delegate added

回答by keerthi chinivar

Below code did solve the problem:

下面的代码确实解决了问题:

If user perform changes to selected image pull only that image otherwise pull original image source without any changes and finally dismiss image picker view controller.

如果用户对所选图像执行更改,则仅拉取该图像,否则拉取原始图像源而不进行任何更改,最后关闭图像选择器视图控制器。

public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]){
    if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
        imageView.image = image
    }
    else if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        imageView.image = image
    } else{
        print("Something went wrong")
    }

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

回答by oyalhi

If you allow editing of the picture imageController.allowsEditing = truethen you will need to get the edited image first:

如果您允许编辑图片,imageController.allowsEditing = true那么您需要先获取编辑后的图像:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    picker.dismissViewControllerAnimated(true, completion: nil)

    if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
        imagePost.image = image
    } else if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        imagePost.image = image
    } else {
        imagePost.image = nil
    }
}

回答by Ashildr

The accepted solutionby Jeetendra Choudharyworks.Although in Xcode 8 with Swift 3 , I noticed that it generates a warning : Instance method 'imagePickerController(_:didFinishPickingMediaWithInfo:)' nearly matches optional requirement 'imagePickerController(_:didFinishPickingMediaWithInfo:)' of protocol 'UIImagePickerControllerDelegate'

接受的溶液通过杰滕德拉乔杜里works.Although在Xcode 8夫特3,我注意到,它生成一个警告: Instance method 'imagePickerController(_:didFinishPickingMediaWithInfo:)' nearly matches optional requirement 'imagePickerController(_:didFinishPickingMediaWithInfo:)' of protocol 'UIImagePickerControllerDelegate'

enter image description here

在此处输入图片说明

and suggests to add either @nonobjcor privatekeyword to silence the warning.If you silence the warning using these suggestions , the solution no longer works though.

并建议添加@nonobjcprivate关键字以消除警告。如果您使用这些建议消除警告,该解决方案将不再有效。

回答by changmeng chen

I found the default images in the photo library could couse this problem. If you drag an image from your computor onto the simulator and choose it. this problem is solved

我发现照片库中的默认图像可能会导致此问题。如果您将计算机中的图像拖到模拟器上并选择它。这个问题解决了

回答by edenPan

According Abdurohman's answer, i change my code and solve the problem,thanks.

根据 Abdurohman 的回答,我更改了代码并解决了问题,谢谢。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage

    photoImageView.image = selectedImage

    // Dismiss the picker.
    dismiss(animated: true, completion: nil)
}

回答by Ali Adil

Add this to viewDidload()

将此添加到 viewDidload()

imagepicker.delegate = self

回答by My Mai

I think you are missing a connection between photoImageView and the image.

我认为您缺少 photoImageView 和图像之间的连接。

Take a look my screenshots below:

看看我下面的截图:

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

回答by My Mai

I think you are missing a connection between photoImageView and the image.

我认为您缺少 photoImageView 和图像之间的连接。

Take a look my screenshots below:

看看我下面的截图:

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

Good luck!

祝你好运!