简体   繁体   中英

Setting picked image from UIImagePicker to UIImageView in Swift

I'm trying to create code so that a user can set an image from their Library or from their Camera Roll and have it set to a ImageView on the screen.

I tried adding this function, however it doesn't seem to work

private func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        picker.dismiss(animated: true, completion: nil)
        //You will get cropped image here..
        if let image = info[editedImage] as UIImage
        {
            self.Picture.image = image
        }
    }

Here is my code as of right now

class NewViewController: UIViewController, UIImagePickerControllerDelegate {

    @IBOutlet var Picture: UIImageView! //the image I am trying to change 

    let imagepicker = UIImagePickerController()

@IBAction func ImagePicker(_ sender: Any) {
        let alert = UIAlertController(title: "Photo", message: "Would you like to choose a picture from your libray, or take a new photo?", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: { action in
            switch action.style{
            case .default:
                self.imagepicker.sourceType = .photoLibrary
                self.imagepicker.allowsEditing = true
                self.imagepicker.delegate = self
                self.present(self.imagepicker, animated: true)
                print("default")

            case .cancel:
                print("cancel")

            case .destructive:
                print("destructive")
            }}))
        alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { action in
            switch action.style{
            case .default:
                self.imagepicker.sourceType = .camera
                self.imagepicker.allowsEditing = true
                self.imagepicker.delegate = self
                self.present(self.imagepicker, animated: true)

                print("default")

            case .cancel:
                print("cancel")

            case .destructive:
                print("destructive")
            }}))
        self.present(alert, animated: true, completion: nil)
    }
}

Why are you trying to use editedImage as a key here? It should be .editedImage

You need to change the method signature as,

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

    picker.dismiss(animated: true, completion: nil)
    guard let image = info[.editedImage] as? UIImage else { return }
    self.Picture.image = image
} 

You can check the method signature here . Currently the method you have is not getting called by the UIImagePickerController delegate because of the wrong parameters. If you remove private keyword from the method, you will get a warning as below which means it looks like the same method but isn't what UIImagePickerController delegate will call.

Instance method 'imagePickerController( :didFinishPickingMediaWithInfo:)' nearly matches optional requirement 'imagePickerController( :didFinishPickingMediaWithInfo:)' of protocol 'UIImagePickerControllerDelegate'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM