简体   繁体   English

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

[英]“Creating an image format with an unknown type is an error” with UIImagePickerController

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中是否更改了此方法的语法或任何内容,或者是否有其他方法可以执行此操作。

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

Remember to add delegate to self 记得向自己添加委托

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

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

If you allow editing of the picture imageController.allowsEditing = true then 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
    }
}

The accepted solution by Jeetendra Choudhary works.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' Jeetendra Choudhary 接受的解决方案很有效 。虽然在Xcode 8中使用Swift 3,我注意到它会生成一个警告: Instance method 'imagePickerController(_:didFinishPickingMediaWithInfo:)' nearly matches optional requirement 'imagePickerController(_:didFinishPickingMediaWithInfo:)' of protocol 'UIImagePickerControllerDelegate'

在此输入图像描述

and suggests to add either @nonobjc or private keyword to silence the warning.If you silence the warning using these suggestions , the solution no longer works though. 并建议添加@nonobjc私有关键字以使警告静音。如果您使用这些建议使警告静音,则解决方案不再有效。

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 这个问题解决了

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

将其添加到viewDidload()

imagepicker.delegate = self

Add "_" in function parameters. 在函数参数中添加“_”。

from

func imagePickerController(picker: UIImagePickerController ...

to

func imagePickerController(_ picker: UIImagePickerController ...

This worked for me: 这对我有用:

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

If this helps anyone, I had this happen when I subclassed UIImageView to create a rounded view. 如果这对任何人都有帮助,那么当我将UIImageView子类化以创建圆角视图时,我就会发生这种情况。 It also happened when I added the below line in viewDidLoad() 当我在viewDidLoad()中添加以下行时也发生了这种情况

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

I ended up adding the line in viewWillLayoutSubViews and it worked. 我最终在viewWillLayoutSubViews中添加了这一行并且它有效。 See this for more details: 有关详细信息,请参阅此

clipsToBounds causes UIImage to not display in iOS10 & XCode 8 clipsToBounds导致UIImage无法在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]

For Xcode 8.1 with swift 3, After changing the delegate method as below 对于带有swift 3的Xcode 8.1,在更改委托方法后如下所示

change your 改变你的

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

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

function to 功能

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

    {


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

}

I forgot to add UINavigationControllerDelegate along with UIImagePickerControllerDelegate in 我忘了添加UINavigationControllerDelegate和UIImagePickerControllerDelegate

class ViewController:UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate

You would have added an variable at the beginning like 你会在一开始就添加一个变量

var imagePicker = UIImagePickerController() var imagePicker = UIImagePickerController()

and setting delegate and call a function in viewdidload() as 并设置委托并在viewdidload()中调用一个函数

 imagePicker.delegate = self
 viwImagePick()

Then explain that function as 然后解释该功能为

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


        }
    }

Now the image is added to image view from the library 现在,图像将添加到库中的图像视图中

I think you are missing a connection between photoImageView and the image. 我认为你错过了photoImageView和图像之间的联系。

Take a look my screenshots below: 看看下面的截图:

在此输入图像描述

在此输入图像描述

I think you are missing a connection between photoImageView and the image. 我认为你错过了photoImageView和图像之间的联系。

Take a look my screenshots below: 看看下面的截图:

在此输入图像描述

在此输入图像描述

Good luck! 祝好运!

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)

}

Be sure to also include the UINavigationControllerDelegate delegate. 一定要包含UINavigationControllerDelegate委托。 This solved the issue for me. 这解决了我的问题。

try below 试试下面

    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
}

This worked for me. 这对我有用。 Just copy and paste it exactly. 只需复制并粘贴即可。

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

What I did was that once I got the image from didFinishPickingMediaWithInfo, I called: 我做的是,一旦我从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(imageData: NSData) {
    imageDatas = imageData
}

to save it in NSData format. 以NSData格式保存。

The console still warned me on "[Generic] Creating an image format with an unknown type is an error" but at least my imageView gets updated to the selected image rather than showing the previous one from viewDidLoad. 控制台仍警告我“[Generic]创建一个包含未知类型的图像格式是一个错误”,但至少我的imageView会更新到所选图像,而不是从viewDidLoad显示前一个图像。

// image picker with collection view class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UICollectionViewDataSource,UICollectionViewDelegate { //带有集合视图类的图像选择器ViewController:UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UICollectionViewDataSource,UICollectionViewDelegate {

@IBOutlet var img: UIImageView!

@IBOutlet weak var collview: UICollectionView!

var image = NSMutableArray()


let imgpkr = UIImagePickerController()

override func viewDidLoad() {
    super.viewDidLoad()

    imgpkr.delegate = self
}




@IBAction func btnselect(_ sender: UIButton) {


    imgpkr.allowsEditing = true // false
    imgpkr.sourceType = .photoLibrary
    imgpkr.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
    present(imgpkr, animated: true, completion: nil)

}


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

    let choose = info[UIImagePickerControllerOriginalImage]as!UIImage
    let edit = info[UIImagePickerControllerEditedImage]as!UIImage

    img.contentMode = .scaleAspectFit
    img.image = edit

    //MARK:- Add image in collview

    image.add(edit)
    collview.reloadData()

    dismiss(animated: true, completion: nil)

}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {

    dismiss(animated: true, completion: nil)
}

//MARK:- Collection View


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return image.count

}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collview.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)as! CollectionViewCell1

    cell.img1.image = image.object(at: indexPath.item)as! UIImage

    return cell



}

For me I was getting the error: 对我来说,我收到了错误:

"fatal error: unexpectedly found nil..." “致命的错误:意外地发现没有...”

when you select imagein imagepicker. 当你选择imagein imagepicker时。

To get around the problem, I created the outlet for the imageView again. 为了解决这个问题,我再次为imageView创建了插座。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建未知类型的图像格式是错误的Objective-C Xcode 8 - Creating an image format with an unknown type is an error Objective-C Xcode 8 创建未知类型的图像格式是错误的…Swift3 - Creating an image format with an unknown type is an error…Swift3 创建未知类型的图像格式是未上传图像的错误。 迅速 - Creating an image format with an unknown type is an error did not upload image. Swift 使用PHAssets时出现此错误:创建具有未知类型的图像格式是一个错误 - While using PHAssets getting this error: Creating an image format with an unknown type is an error 通过WKWebView显示WebApp时,“创建未知类型的图像格式是错误的” - “Creating an image format with an unknown type is an error” when displaying a WebApp through WKWebView UIImagePickerController摄像机类型返回黑色图像 - UIImagePickerController camera type returning black image 将使用 UIImagePickerController 拍摄的图像以 HEIC 格式保存到照片胶卷中 - Save image taken with UIImagePickerController to photo roll in HEIC format 图像以未知格式保存到 Firebase - Image is saved in an unknown format to the Firebase UIImagePickerController出错 - Error with UIImagePickerController 未知类型错误 - Unknown type error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM