Cameron Charmanson
Cameron Charmanson

Reputation: 11

Cannot force unwrap value of non-optional type '(UIImagePickerController, [UIImagePickerController.InfoKey : UIImage]) -> ()'

I was working on my code when I got this error message at the part setProfilePicture : Cannot force unwrap value of non-optional type '(UIImagePickerController, [UIImagePickerController.InfoKey : UIImage]) -> ()' Could somebody tell me what happened and how to fix it ?

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

Upvotes: 0

Views: 754

Answers (2)

Sujit Nachan
Sujit Nachan

Reputation: 204

try this:

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

Upvotes: 2

Nathan Day
Nathan Day

Reputation: 6037

You second argument to the func setProfilePicture(imageView:imageToSet: is profileImage! which is the forced unwrapping of the function you have declared above as not being optional.

Upvotes: 0

Related Questions