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

在 iOS10Swift 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或有任何其他方法这样做。

53976 次浏览

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

Add "_" in function parameters.

from

func imagePickerController(picker: UIImagePickerController ...

to

func imagePickerController(_ picker: UIImagePickerController ...

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

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

Remember to add delegate to self

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

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

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

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. It also happened when I added the below line in viewDidLoad()

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

I ended up adding the line in viewWillLayoutSubViews and it worked. See this for more details:

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
}

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'

enter image description here

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.

Be sure to also include theUINavigationControllerDelegate delegate. This solved the issue for me.

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

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
}

Change didFinishPickingMediaWithInfo info: [String : AnyObject] , to didFinishPickingMediaWithInfo info: [String : Any]

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:

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.

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.

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

For Xcode 8.1 with swift 3, After changing the delegate method as below

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

class ViewController:UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate

You would have added an variable at the beginning like

var imagePicker = UIImagePickerController()

and setting delegate and call a function in viewdidload() as

 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

Add this to viewDidload()

imagepicker.delegate = self

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

Take a look my screenshots below:

enter image description here

enter image description here

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

Take a look my screenshots below:

enter image description here

enter image description here

Good luck!

// image picker with collection view class 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






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


}

对我来说,我得到了错误:

"fatal error: unexpectedly found nil..."

when you select imagein imagepicker.

To get around the problem, I created the outlet for the imageView again.