"Creating an image format with an unknown type is an error" 与 UIImagePickerController
"Creating an image format with an unknown type is an error" with UIImagePickerController
在 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)
}
图像未被选择和更新。我需要帮助或建议来了解 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)
}
在函数参数中添加“_”。
来自
func imagePickerController(picker: UIImagePickerController ...
至
func imagePickerController(_ picker: UIImagePickerController ...
如果您允许编辑图片 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
}
}
记得给自己加delegate
let picker = UIImagePickerController()
picker.delegate = self // delegate added
根据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)
}
这对我有用:
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 以创建圆形视图时遇到过这种情况。当我在 viewDidLoad()
中添加以下行时也会发生这种情况
imageView.layer.cornerRadius = self.imageView.frame.size.width / 2
我最终在 viewWillLayoutSubViews 中添加了该行并且它起作用了。有关详细信息,请参阅此内容:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.dismiss(animated: true, completion: nil)
self.imageTook.image = image
}
by Jeetendra Choudhary works.Although in Xcode 8 和 Swift 3 ,我注意到它会生成警告:
Instance method 'imagePickerController(_:didFinishPickingMediaWithInfo:)' nearly matches optional requirement 'imagePickerController(_:didFinishPickingMediaWithInfo:)' of protocol 'UIImagePickerControllerDelegate'
并建议添加 @nonobjc 或 private 关键字使 warning.If 静音 您使用这些建议使警告静音,但该解决方案不再有效。
一定要包括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
}
改变didFinishPickingMediaWithInfo info: [String : AnyObject]
,
至 didFinishPickingMediaWithInfo info: [String : Any]
这对我有用。
只需准确复制并粘贴即可。
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 格式。
控制台仍然警告我“[通用] 创建类型未知的图像格式是错误的”,但至少我的 imageView 更新为所选图像,而不是显示来自 viewDidLoad 的前一个图像。
下面的代码确实解决了问题:
如果用户对所选图像进行更改,则仅拉取该图像,否则拉取原始图像源而不做任何更改,最后关闭图像选择器视图控制器。
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)
}
对于 Xcode 8.1 和 swift 3,如下更改委托方法后
改变你的
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
imagePost.image = image
self.dismiss(animated: true, completion: nil)
}
作用于
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
imagePost.image = info[UIImagePickerControllerOriginalImage] as? UIImage
picker.dismiss(animated: true, completion: nil)
}
我忘记在
中添加 UINavigationControllerDelegate 和 UIImagePickerControllerDelegate
class ViewController:UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate
你会像
一样在开头添加一个变量
var imagePicker = UIImagePickerController()
并在 viewdidload() 中设置委托并调用函数
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)
}
}
现在图像已从库中添加到图像视图
将此添加到 viewDidload()
imagepicker.delegate = self
我认为您缺少 photoImageView 和图像之间的联系。
看看我下面的截图:
我认为您缺少 photoImageView 和图像之间的联系。
看看我下面的截图:
祝你好运!
// 带集合视图的图像选择器
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..."
当您 select 在图像选择器中输入图像时。
为了解决这个问题,我再次为 imageView
创建了插座。
在 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)
}
图像未被选择和更新。我需要帮助或建议来了解 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)
}
在函数参数中添加“_”。
来自
func imagePickerController(picker: UIImagePickerController ...
至
func imagePickerController(_ picker: UIImagePickerController ...
如果您允许编辑图片 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
}
}
记得给自己加delegate
let picker = UIImagePickerController()
picker.delegate = self // delegate added
根据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)
}
这对我有用:
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 以创建圆形视图时遇到过这种情况。当我在 viewDidLoad()
中添加以下行时也会发生这种情况imageView.layer.cornerRadius = self.imageView.frame.size.width / 2
我最终在 viewWillLayoutSubViews 中添加了该行并且它起作用了。有关详细信息,请参阅此内容:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.dismiss(animated: true, completion: nil)
self.imageTook.image = image
}
Instance method 'imagePickerController(_:didFinishPickingMediaWithInfo:)' nearly matches optional requirement 'imagePickerController(_:didFinishPickingMediaWithInfo:)' of protocol 'UIImagePickerControllerDelegate'
并建议添加 @nonobjc 或 private 关键字使 warning.If 静音 您使用这些建议使警告静音,但该解决方案不再有效。
一定要包括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
}
改变didFinishPickingMediaWithInfo info: [String : AnyObject]
,
至 didFinishPickingMediaWithInfo info: [String : Any]
这对我有用。 只需准确复制并粘贴即可。
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 格式。
控制台仍然警告我“[通用] 创建类型未知的图像格式是错误的”,但至少我的 imageView 更新为所选图像,而不是显示来自 viewDidLoad 的前一个图像。
下面的代码确实解决了问题:
如果用户对所选图像进行更改,则仅拉取该图像,否则拉取原始图像源而不做任何更改,最后关闭图像选择器视图控制器。
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)
}
对于 Xcode 8.1 和 swift 3,如下更改委托方法后
改变你的
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
imagePost.image = image
self.dismiss(animated: true, completion: nil)
}
作用于
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
imagePost.image = info[UIImagePickerControllerOriginalImage] as? UIImage
picker.dismiss(animated: true, completion: nil)
}
我忘记在
中添加 UINavigationControllerDelegate 和 UIImagePickerControllerDelegateclass ViewController:UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate
你会像
一样在开头添加一个变量var imagePicker = UIImagePickerController()
并在 viewdidload() 中设置委托并调用函数
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)
}
}
现在图像已从库中添加到图像视图
将此添加到 viewDidload()
imagepicker.delegate = self
我认为您缺少 photoImageView 和图像之间的联系。
看看我下面的截图:
我认为您缺少 photoImageView 和图像之间的联系。
看看我下面的截图:
祝你好运!
// 带集合视图的图像选择器 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..."
当您 select 在图像选择器中输入图像时。
为了解决这个问题,我再次为 imageView
创建了插座。