无法强制打开非可选类型'(UIImagePickerController,[UIImagePickerController.InfoKey:UIImage])->()'的展开值。

时间:2018-10-20 09:03:45

标签: ios xcode

当我在setProfilePicture部分收到此错误消息时,我正在处理代码:无法强制打开非可选类型'(UIImagePickerController,[UIImagePickerController.InfoKey:UIImage])->()'的包装值。 有人可以告诉我发生了什么事以及如何解决吗?

//Selecting image from Gallery
@objc func imagePickerController(_ picker: UIImagePickerController,  didFinishPickingMediaWithInfo info: [String : Any]){

    //let profileImage =
    func profileImage(_ picker: UIImagePickerController,
                      didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : UIImage]){
        }
    setProfilePicture(imageView: self.imageSelector, imageToSet: profileImage)

    self.dismiss(animated: true, completion: nil)

}

// Displaying Profile Picture on the Image View
internal func setProfilePicture(imageView: UIImageView, imageToSet: UIImage){
    print("setProfilePicture called")
    imageView.layer.cornerRadius = 40
    imageView.layer.borderColor = UIColor.white.cgColor
    imageView.layer.masksToBounds = true
    imageView.image = imageToSet
}
}

2 个答案:

答案 0 :(得分:2)

尝试一下:

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

        if let image = info[.originalImage] as? UIImage {
            setProfilePicture(imageView: self.imageSelector, imageToSet: image)
            dismiss(animated: true, completion: nil)
        }
    }

答案 1 :(得分:0)

函数setProfilePicture(imageView:imageToSet:is profileImage!的第二个参数是profileImage !,这是您在上面声明为非可选函数的强制展开。