尝试允许用户从其照片库中选择照片,然后在UIImageView中显示该图像。照片库显示得很好,但是当我从我的库中选择一张照片时,我得到了这个错误" [Generic]创建一个未知类型的图像格式是一个错误"。
逐步完成下面的代码,但只有在选择这两个函数中没有出现的图像时才会出现错误。
@IBAction func openPhotoLibraryButton(sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary;
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage!
let imageData = UIImageJPEGRepresentation(image!, 0.6)
let compressedJPGImage = UIImage(data: imageData!)
imagePicked.image = compressedJPGImage
}
尝试了这里建议的解决方案:xCode 8 - Creating an image format with an unknown type is an error但它们都没有奏效,听起来有几个人没有解决这个问题。有任何想法吗?
答案 0 :(得分:0)
您是否尝试在选择器之前添加_
?我相信该方法已更改,您正在使用的方法已弃用,您应该可以让它自动完成。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
if let imageData = UIImageJPEGRepresentation(image, 0.6) {
let compressedJPGImage = UIImage(data: imageData)
imagePicked.image = compressedJPGImage
}
} else {
print("Image Picker Failure")
//handle any possible image picker issues/failures so that app doesn't crash in the future
//troubleshoot further if it always hits this
}
dismissViewControllerAnimated(true, completion: nil)
}