Swift 4.2无法将类型'(UIImagePickerController.InfoKey).Type'的值转换为预期的参数类型'UIImagePickerController.InfoKey'

时间:2018-06-19 12:51:24

标签: ios swift uiimagepickercontroller ios12

我的以下代码在swift 4上运行正常,但是升级到swift 4.2之后,出现此错误,我浪费了3个小时来查找问题所在,但失败了。请任何人指导我如何解决此问题。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

    if postType == 2 {
        let image = info[convertFromUIImagePickerControllerInfoKey(UIImagePickerController.InfoKey)] as! UIImage
        mediaType.image = image
    } else {
        videoURL = info[convertFromUIImagePickerControllerInfoKey(UIImagePickerController.InfoKey.mediaURL)] as? URL
        do {
            let asset = AVURLAsset(url: videoURL!, options: nil)
            let imgGenerator = AVAssetImageGenerator(asset: asset)
            imgGenerator.appliesPreferredTrackTransform = true
            let cgImage = try imgGenerator.copyCGImage(at: CMTime.init(value: 0, timescale: 1), actualTime: nil)
            let thumbnail = UIImage(cgImage: cgImage)
            self.mediaType.image = thumbnail
        } catch {
            print("*** Error generating thumbnail: \(error)")
        }
    }
    picker.dismiss(animated: true, completion: nil)
}

6 个答案:

答案 0 :(得分:19)

你可以这样写...

if let image = info[.originalImage] as? UIImage {

    print("image found")
    //do something with an image

} else {
     print("Not able to get an image")
}

答案 1 :(得分:5)

info[convertFromUIImagePickerControllerInfoKey(UIImagePickerController.InfoKey)]

没有任何意义。您要指定整个枚举类型InfoKey而不是特定值,例如:

info[convertFromUIImagePickerControllerInfoKey(UIImagePickerController.InfoKey.originalImage)]

可能也可以这样写:

info[.originalImage] as! UIImage

答案 2 :(得分:2)

您可以按如下方式访问属性。

var editedImage = (info[UIImagePickerControllerEditedImage] as? UIImage)!
var originalImage = (info[UIImagePickerControllerOriginalImage] as? UIImage)!

答案 3 :(得分:1)

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
{
    // Local variable inserted by Swift 4.2 migrator.
    let info = convertFromUIImagePickerControllerInfoKeyDictionary(info)

    if picker.sourceType == .photoLibrary || picker.sourceType == .camera
    {
        let img: UIImage = info[convertFromUIImagePickerControllerInfoKey(UIImagePickerController.InfoKey.editedImage)] as! UIImage
        EditedImage=img
        WAProfile_UserImageView.image=EditedImage
        picker.dismiss(animated: true, completion: nil)
    }
}

// Helper function inserted by Swift 4.2 migrator.
fileprivate func convertFromUIImagePickerControllerInfoKeyDictionary(_ input: [UIImagePickerController.InfoKey: Any]) -> [String: Any] {
return Dictionary(uniqueKeysWithValues: input.map {key, value in (key.rawValue, value)})}

// Helper function inserted by Swift 4.2 migrator.
fileprivate func convertFromUIImagePickerControllerInfoKey(_ input: UIImagePickerController.InfoKey) -> String {
return input.rawValue}

答案 4 :(得分:0)

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

答案 5 :(得分:0)

虽然是旧线程,但迁移器添加了此功能,似乎可以很好地解决问题:

// Function inserted by Swift 4.2 migrator.
fileprivate func convertFromUIImagePickerControllerInfoKeyDictionary(_ input: 
    [UIImagePickerController.InfoKey: Any]) -> [String: Any] {
    return Dictionary(uniqueKeysWithValues: input.map {key, value in (key.rawValue, value)})
}