UIImagePickerController“创建未知类型的图像格式是错误”


76

在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中是否已更改,或者是否有其他方法可以执行。


8
将其从AnyObject更改为AnydidFinishPickingMediaWithInfo info: [String : Any]
Leo Dabus

2
您应该让Xcode为您自动完成它。您正在使用,我认为该方法已被弃用
狮子座Dabus

1
谢谢Leo确实有帮助。
杰特(Jeet)

5
同样的问题,以及字串:似乎无济于事
TheeBen 16'9

1
还是行不通 。我使用的是8.0版(8A218a)
Kegham K.

Answers:


28

下面提到的代码确实为我解决了问题-

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

21
这不能为我解决问题。我更新了该函数,但在调用此函数时仍会立即收到错误消息。在那之后使用UIImage无效。
蒂姆(Tim)

3
@Tim我仍然会收到错误消息,但是访问图像的效果很好。
MobileVet

1
@jeetendra这确实解决了我的问题,但仍然给我一个警告“实例方法(didFinishPickingMediaWithInfo)几乎符合其可选要求。” 当我在函数之前添加private或nonobj时,警告消失了,但该方法未调用。
Arpit Dhamane

此解决方案不适用于XCode 8和Swift3。我使用的是iOS10设备。
马查多

1
尽管这确实帮助我解决了图像不出现的问题,但是错误仍然出现在控制台中。不过谢谢
Arjun Kalidas'Mar

11

记得给自己添加委托

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

哈,你救了我。Xcode 7可以在没有委托分配的情况下正确地编译和运行。
MobileVet

10

下面的代码确实解决了问题:

如果用户对所选图像执行更改,则仅拉该图像,否则不进行任何更改就拉原始图像源,最后关闭图像选择器视图控制器。

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

2
欢迎使用Stack Overflow!仅代码答案本身并不是很有用。如果您可以添加一些细节来说明它如何/为什么回答了问题,这将有所帮助
Jeet

这应该是正确的答案。完美地工作。
user3286381 '17

7

如果您允许编辑图片,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
    }
}

我已执行您的回复,但仍然出现错误。您能否阅读我的问题stackoverflow.com/questions/50189732
bibscy

5

接受的溶液通过杰滕德拉乔杜里works.Although在Xcode 8夫特3,我注意到,它生成一个警告: Instance method 'imagePickerController(_:didFinishPickingMediaWithInfo:)' nearly matches optional requirement 'imagePickerController(_:didFinishPickingMediaWithInfo:)' of protocol 'UIImagePickerControllerDelegate'

在此处输入图片说明

并建议添加 @nonobjcprivate关键字以使警告静音。如果使用这些建议使警告静音,该解决方案将不再起作用。


感谢您的澄清。
截拳道

1
问题是“ AnyObject”-正确的Swift 3签名使用“ Any”。不要使用Xcode建议的解决方案来静默警告。
Zig

5

我发现照片库中的默认图像可能会解决此问题。如果将图像从计算机拖到模拟器上并选择它。这个问题解决了


3

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

那是在Swift 3.0中对我有用的唯一代码。谢谢!
fraxool


1

在函数参数中添加“ _”。

func imagePickerController(picker: UIImagePickerController ...

func imagePickerController(_ picker: UIImagePickerController ...

1

这为我工作:

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

欢迎使用Stack Overflow!仅代码答案本身并不是很有用。如果您可以添加一些详细信息来说明它如何/为什么回答了问题,这将有所帮助
Jeet

1

如果这对任何人都有帮助,则在将UIImageView子类化以创建圆形视图时发生了这种情况。当我在viewDidLoad()中添加以下行时,也会发生这种情况

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

我最终在viewWillLayoutSubViews中添加了这一行,并且行得通。请参阅此以获取更多详细信息:

clipsToBounds导致UIImage无法在iOS10和XCode 8中显示


1
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

         let image = info[UIImagePickerControllerOriginalImage] as? UIImage
        self.dismiss(animated: true, completion: nil)
        self.imageTook.image = image
    }

4
欢迎使用Stack Overflow!仅代码答案本身并不是很有用。如果您可以添加一些详细信息来解释它如何/为什么回答问题,这将有所帮助。
SiHa

2
这解决了我的问题。但是你能解释一下原因吗?
Krishnadas PC

1

更改didFinishPickingMediaWithInfo info: [String : AnyObject]didFinishPickingMediaWithInfo info: [String : Any]


1

对于带有swift 3的Xcode 8.1,按如下所示更改委托方法后

改变你的

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)

}

我忘了在UIImagePickerControllerDelegate中添加UINavigationControllerDelegate

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)


        }
    }

现在将图像从库添加到图像视图


请编辑答案以说明这与实际问题有何关系。从您的解释中还不清楚。
截拳

1

我认为您缺少photoImageView和图像之间的连接。

在下面看我的屏幕截图:

在此处输入图片说明

在此处输入图片说明


为什么2回答内容相同?
पवन

1

我认为您缺少photoImageView和图像之间的连接。

在下面看我的屏幕截图:

在此处输入图片说明

在此处输入图片说明

祝好运!


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

}


0

试试下面

    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

这对我有用。只需复制并准确粘贴即可。

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

5
欢迎使用Stack Overflow!仅代码答案本身并不是很有用。如果您可以添加一些详细信息来说明它如何/为什么回答了问题,这将有所帮助
Jeet

0

我所做的是,一旦我从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中显示前一个图像。


0

//具有集合视图类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



}

-1

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

“致命错误:意外发现零...”

当您在图像选择器中选择图像时。

为了解决这个问题,我imageView再次创建了出口。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.