使用UIImagePickerController“使用未知类型创建图像格式是一个错误”

问题描述 投票:76回答:23

在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中是否更改了此方法的语法或任何内容,或者是否有其他方法可以执行此操作。

ios swift xcode uiimageview xcode8
23个回答
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)
}

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

1
投票

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

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

我最终在viewWillLayoutSubViews中添加了这一行并且它有效。有关详细信息,请参阅此

clipsToBounds causes UIImage to not display in 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
    }

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)

}

我忘了添加UINavigationControllerDelegate和UIImagePickerControllerDelegate

class ViewController:UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate

你会在一开始就添加一个变量

是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和图像之间的联系。

看看下面的截图:

enter image description here

enter image description here


1
投票

我认为你错过了photoImageView和图像之间的联系。

看看下面的截图:

enter image description here

enter image description here

祝好运!


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
投票

一定要包括UINavigationControllerDelegate代表。这解决了我的问题。


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
}

11
投票

记得向自己添加委托

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

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

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格式保存。

控制台仍警告我“[Generic]创建一个包含未知类型的图像格式是一个错误”,但至少我的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
投票

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

“致命的错误:意外地发现没有...”

当你选择imagein imagepicker时。

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


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

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

5
投票

accepted solutionJeetendra Choudhary工作。虽然在Xcode 8中使用Swift 3,我注意到它会产生一个警告:Instance method 'imagePickerController(_:didFinishPickingMediaWithInfo:)' nearly matches optional requirement 'imagePickerController(_:didFinishPickingMediaWithInfo:)' of protocol 'UIImagePickerControllerDelegate'

enter image description here

并建议添加@nonobjc或私有关键字以使警告静音。如果您使用这些建议使警告静音,则解决方案不再有效。


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

2
投票

将其添加到viewDidload()

imagepicker.delegate = self

1
投票

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

func imagePickerController(picker: UIImagePickerController ...

func imagePickerController(_ picker: UIImagePickerController ...
© www.soinside.com 2019 - 2024. All rights reserved.