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

When I select an image from an image picker in iOS 10 Swift 3, I get an error message - 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 will not be selected and updated. I need help or a suggestion to find out 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.

+73
ios xcode swift xcode8 uiimageview
Aug 18 '16 at 4:14
source share
22 answers

Below mentioned code really solved 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) } 
+27
Aug 18 '16 at 8:34
source share

Remember to add a delegate to yourself

 let picker = UIImagePickerController() picker.delegate = self // delegate added 
+11
Sep 25 '16 at 21:23
source share

The code below fixed the problem:

If the user makes changes to the selected image, pull out that image only, otherwise pull out the original image source without any changes and finally unscrew the image 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) } 
+9
Dec 15 '16 at 14:13
source share

If you enable image editing imageController.allowsEditing = true , you must first edit the image:

 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 } } 
+7
Sep 25 '16 at 6:10
source share

the decision made by Jeetendra Choudhary is working. 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 adding either @nonobjc or a personal keyword to disable the warning. If you turn off the warning using these suggestions, the solution no longer works.

+5
Oct 12 '16 at 8:24
source share

I found that the default images in the photo library can solve this problem. If you drag an image from your companion to the simulator and select it. this problem is resolved.

+5
Oct. 27 '16 at 8:08
source share

According to Abdurohman, 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) } 
+3
Sep 29 '16 at 7:57
source share

Add this to view Didload ()

 imagepicker.delegate = self 
+2
May 22 '17 at 17:20
source share

Add "_" to the function parameters.

from

 func imagePickerController(picker: UIImagePickerController ... 

to

 func imagePickerController(_ picker: UIImagePickerController ... 
+1
Sep 21 '16 at 7:24
source share

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) } } 
+1
Sep 30 '16 at 23:58
source share

If this helps anyone, I had this when I subclassed UIImageView to create a rounded view. This also happened when I added the following line to viewDidLoad ()

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

I ended up adding a line to viewWillLayoutSubViews and it worked. See This for more details:

clipsToBounds causes UIImage not to display in iOS10 and Xcode 8

+1
01 Oct '16 at 18:14
source share
 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { let image = info[UIImagePickerControllerOriginalImage] as? UIImage self.dismiss(animated: true, completion: nil) self.imageTook.image = image } 
+1
Oct 03 '16 at 7:51
source share

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

+1
Nov 17 '16 at 4:25
source share

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

change your

 func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) { imagePost.image = image self.dismiss(animated: true, completion: nil) } 

for

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

I forgot to add a UINavigationControllerDelegate along with a UIImagePickerControllerDelegate in

 class ViewController:UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate 

You would add a variable at the beginning, for example

var imagePicker = UIImagePickerController ()

and set the delegate and call the function in viewdidload () as

  imagePicker.delegate = self viwImagePick() 

Then explain that the function <

  //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 the image view from the library

+1
Apr 12 '17 at 12:18
source share

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

Check out my screenshots below:

enter image description here

enter image description here

Good luck

+1
Jul 23 '17 at 6:55
source share
 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) } 
+1
Dec 06 '17 at 10:34 on
source share

Be sure to include the UINavigationControllerDelegate delegate. This solved the problem for me.

0
Oct. 19 '16 at 11:04 on
source share

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 } 
0
Nov 14 '16 at 18:59
source share

It worked for me. Just copy and paste it for sure.

  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) } 
0
Nov 26 '16 at 1:26
source share

What I did was that as soon as 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: "[Generic] Creating an image format with an unknown type is an error," but at least my ImageView is being updated to the selected image, and not showing the previous one from viewDidLoad.

0
Dec 09 '16 at 4:31
source share

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

Check out my screenshots below:

enter image description here

enter image description here

0
Jul 23 '17 at 6:54
source share

// 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 } 
0
Nov 10 '17 at 6:40
source share



All Articles