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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用UIImagePickerController“使用未知类型创建图像格式是一个错误”相关的知识,希望对你有一定的参考价值。

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

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

答案

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

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)
}
另一答案

这对我有用:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        imageView.image = image
        self.dismiss(animated: true, completion: nil)
    }
}
另一答案

如果这对任何人都有帮助,那么当我将UIImageView子类化以创建圆角视图时,我就会发生这种情况。当我在viewDidLoad()中添加以下行时也发生了这种情况

imageView.layer.cornerRadius = self.imageView.frame.size.width / 2

我最终在viewWillLayoutSubViews中添加了这一行并且它有效。有关详细信息,请参阅此

clipsToBounds causes UIImage to not display in iOS10 & XCode 8

另一答案
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

         let image = info[UIImagePickerControllerOriginalImage] as? UIImage
        self.dismiss(animated: true, completion: nil)
        self.imageTook.image = image
    }
另一答案

didFinishPickingMediaWithInfo info: [String : AnyObject]改为didFinishPickingMediaWithInfo info: [String : Any]

另一答案

对于带有swift 3的Xcode 8.1,在更改委托方法后如下所示

改变你的

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

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

功能

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

    {


imagePost.image = info[UIImagePickerControllerOriginalImage] as? UIImage
 picker.dismiss(animated: true, completion: nil)

}

我忘了添加UINavigationControllerDelegate和UIImagePickerControllerDelegate

class ViewController:UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate

你会在一开始就添加一个变量

是imagePicker = UIImagePickerController()

并设置委托并在viewdidload()中调用一个函数

 imagePicker.delegate = self
 viwImagePick()

然后解释该功能为

  //ImagePicker
    func viwImagePick(){


        let alert = UIAlertController(title: nil, message: "Choose your source", preferredStyle: UIAlertControllerStyle.alert)

        alert.addAction(UIAlertAction(title: "Camera", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in
            print("Camera selected")
            self.openCamera()

            //Code for Camera
            //cameraf
        })
        alert.addAction(UIAlertAction(title: "Photo library", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in
            print("Photo selected")
            self.openGallary()

            //Code for Photo library
            //photolibaryss
        })


        self.present(alert, animated: true, completion: nil)
           }



    func openCamera()
    {
        imagePicker.sourceType = UIImagePickerControllerSourceType.camera
        if UIDevice.current.userInterfaceIdiom == .phone
        {
            self.present(imagePicker, animated: true, completion: nil)
        }
        else
        {
            let popover = UIPopoverController(contentViewController: imagePicker)

            popover.present(from: profileImgViw.frame, in: self.view, permittedArrowDirections: UIPopoverArrowDirection.any, animated: true)

        }
    }

    func openGallary()
    {
        imagePicker.sourceType = UIImagePickerControllerSourceType.savedPhotosAlbum
        if UIDevice.current.userInterfaceIdiom == .phone
        {
            self.present(imagePicker, animated: true, completion: nil)
        }
        else
        {
           let popover = UIPopoverController(contentViewController: imagePicker)

            popover.present(from: profileImgViw.frame, in: self.view, permittedArrowDirections: UIPopoverArrowDirection.any, animated: true)


        }
    }

现在,图像将添加到库中的图像视图中

另一答案

我认为你错过了photoImageView和图像之间的联系。

看看下面的截图:

enter image description here

enter image description here

另一答案

我认为你错过了photoImageView和图像之间的联系。

看看下面的截图:

enter image description here

enter image description here

祝好运!

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

    picker.dismiss(animated: true, completion: nil)

}
另一答案

一定要包括UINavigationControllerDelegate代表。这解决了我的问题。

另一答案

试试下面

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    print("image selected");
    let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImag
    self.dismiss(animated: true, completion: nil)
   UIImg.image = selectedImage
}
另一答案

记得向自己添加委托

let picker = UIImagePickerController()
picker.delegate = self // delegate added
另一答案

这对我有用。只需复制并粘贴即可。

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    // The info dictionary contains multiple representations of the image, and this uses the original.
        let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage

    // Set photoImageView to display the selected image.
     photoImageView.image = selectedImage

    // Dismiss the picker.
    dismiss(animated: true, completion: nil)
}
另一答案

我做的是,一旦我从didFinishPickingMediaWithInfo获得图像,我打电话给:

func prepareImageForSaving(image:UIImage) {
    // create NSData from UIImage
    guard let imageData = UIImageJPEGRepresentation(image, 1) else {
        // handle failed conversion
        print("jpg error")
        return
    }

    self.saveImage(imageData: imageData as NSData)
}

func saveImage(

以上是关于使用UIImagePickerController“使用未知类型创建图像格式是一个错误”的主要内容,如果未能解决你的问题,请参考以下文章

如何使用/用于 UIImagePickerController 显示所有相机控件?

iOS:UIImagePickerController - 使用工具栏属性?

使用 UIImagePickerController 保存图像

将 UIImagePickerController 与 Web 数据源一起使用

无法使用 UIImagePickerController 定位图像

iOS SDK - 使用 UIImagePickerController 时如何恢复状态栏?