5

使用 UIImagePicker 选择图像时出现错误:

[Generic] Creating an image format with an unknown type is an error

我正在使用 Xcode 8.1 和 Swift 3。我已经在网上搜索了所有内容,但似乎没有任何问题可以解决我的问题,请帮忙!

这是我的代码:

class TabBarController: UITabBarController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

var isSelected: Bool = false
var imgPicker: UIImagePickerController = UIImagePickerController()

override func viewDidLoad() {
    super.viewDidLoad()
    //imgPicker = UIImagePickerController()
    imgPicker.delegate = self
    imgPicker.allowsEditing = false
    imgPicker.sourceType = .photoLibrary
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    imgPicker.dismiss(animated: true, completion: nil)

    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        self.performSegue(withIdentifier: "toPostPicture", sender: image)
    }
}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    dismiss(animated: true, completion: nil)
}

func askImagePickerSource(sender: UIViewController) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
        let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

        let cameraAction = UIAlertAction(title: "Camera", style: .default, handler: { (action: UIAlertAction) in
            self.imgPicker.sourceType = .camera
            self.imgPicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .camera)!
            sender.present(self.imgPicker, animated: true, completion: nil)
        })
        let photoLibraryAction = UIAlertAction(title: "Photo Library", style: .default, handler: { (action: UIAlertAction) in
            self.imgPicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
            sender.present(self.imgPicker, animated: true, completion: nil)
        })
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (UIAlertAction) in
            sender.dismiss(animated: true, completion: nil)
        }

        alert.addAction(cameraAction)
        alert.addAction(photoLibraryAction)
        alert.addAction(cancelAction)

        sender.present(alert, animated: true, completion: nil)
    } else {
        self.imgPicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
        sender.present(self.imgPicker, animated: true, completion: nil)
    }
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "toPostPicture" {
        if let postPictureVC = segue.destination as? PostPictureVC {
            if let image = sender as? UIImage {
                postPictureVC.image = image
            }
        }
    }
}

}

4

2 回答 2

3

我没有为 UIImagePickerController添加委托作为' self ',我遇到了同样的问题。添加后,我能够从图库中获取选定的图像。但即使将委托添加为self并能够访问图像(在 ImageView 上显示),我仍然遇到同样的错误。经过对 SO 的进一步研究,我知道这只是一个错误,不会以任何方式影响应用程序。

另请参阅“使用未知类型创建图像格式是错误 Objective-C Xcode 8”

于 2017-02-19T11:54:33.640 回答
0

你可以在你的选择器委托中发布参数吗.debugDescriptioninfo

func imagePickerController(_ picker: UIImagePickerController,
                       didFinishPickingMediaWithInfo info: [String : Any]) {
print(info.debugDescription)
...
}
于 2016-11-02T06:43:57.487 回答