Xcode 8 – 创build一个未知types的图像格式是一个错误
在iOS 10中从图像select器中select图像时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) }
图像没有被选中和更新。 我需要帮助或build议,以了解是否在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) }
记得将代表添加到自我
let picker = UIImagePickerController() picker.delegate = self // delegate added
下面的代码确实解决了问题:
如果用户对所选图像执行更改,则仅拉取该图像,否则将原始图像源无任何更改地拖动,最后closures图像选取器视图控制器。
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) }
如果您允许编辑图片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 } }
尽pipe在使用Swift 3的Xcode 8中,我注意到它产生了一个警告: Instance method 'imagePickerController(_:didFinishPickingMediaWithInfo:)' nearly matches optional requirement 'imagePickerController(_:didFinishPickingMediaWithInfo:)' of protocol 'UIImagePickerControllerDelegate'
并build议添加@nonobjc或private关键字来使警告消失。如果您使用这些build议使警告消失 ,解决scheme将不再起作用。
我发现照片库中的默认图像可以解决这个问题。 如果您将图像从计算机拖放到模拟器上并select它。 这个问题解决了
根据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
在函数参数中添加“_”。
从
func imagePickerController(picker: UIImagePickerController ...
至
func imagePickerController(_ picker: UIImagePickerController ...
这对我工作:
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创build为一个四舍五入的视图时,发生了这种情况。 当我在viewDidLoad()中添加下面的代码时,
imageView.layer.cornerRadius = self.imageView.frame.size.width / 2
我结束了在viewWillLayoutSubViews中添加行,它的工作。 看到这个更多的细节:
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]
对于使用swift 3的Xcode 8.1,在如下所示更改委托方法之后
改变你的
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) { imagePost.image = image self.dismiss(animated: true, completion: nil) }
function
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { imagePost.image = info[UIImagePickerControllerOriginalImage] as? UIImage picker.dismiss(animated: true, completion: nil) }
我忘了在UIImagePickerControllerDelegate中添加UINavigationControllerDelegate
class ViewController:UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate
你会在开始的时候添加一个variables
var imagePicker = UIImagePickerController()
并设置委托并在viewdidload()中调用函数as
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和图像之间的连接。
看看我的截图如下:
祝你好运!
一定还要包含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 }
这对我有效。 只需复制并粘贴它。
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(imageData: NSData) { imageDatas = imageData }
将其保存为NSData格式。
控制台仍然警告我“[通用]创build一个未知types的图像格式是一个错误”,但至less我的imageView更新到选定的图像,而不是显示从viewDidLoad以前的一个。
我想你是错过了photoImageView和图像之间的连接。
看看我的截图如下:
//图像select器与集合视图类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 }
将AnyObject
更改为Any
并且不要在函数imagePickerController
使用private。