Displaying selected image on screen using UIIamgePickerControllerDelegate in Swift

问题内容:

I am attempting to learn more about the delegate pattern. I have written an app that accesses the user’s camera roll and I want to display that image on the view controller after the user selects the image. Here is what I have thus far:

import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var imagePickerView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    // Delegate Methods
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
            imagePickerView.image = image
        }
    }

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

    @IBAction func pickAnImage(_ sender: Any) {

        let pickerController = UIImagePickerController()
        present(pickerController, animated: true, completion: nil)
    }
}

The camera roll appears as expected and the Cancel buttons dismiss the modal. Selecting an image also dismisses the modal, however that image is not displayed on the screen. I have confirmed that my imagePickerController is corrected referenced as an outlet. What am I missing?

问题评论:

1
you are missing pickerController.delegate = self before the present

答案:

答案1:

You have to dismiss the controller once the image is selected, maybe the following detailed function will help you:

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

    var selectedImageFromPicker: UIImage?
    if let editedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
        selectedImageFromPicker = editedImage
    } else if let originalImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        selectedImageFromPicker = originalImage
    }

    if let selectedImage = selectedImageFromPicker {
        imageView.image = selectedImage
    }
    dismiss(animated: true, completion: nil)
}

答案评论:

Yea, the dismissal of the controller was a step I was missing. That, and the assignment of the delegate. Beginner mistakes.
– Andrew Tuzson
4 hours ago
Good, keep coding and do more research before asking here, or else someone could just mark your question as duplicate.

答案2:

You need to set the view controller as the delegate of pickerController

// pickerController.delegate = self

Also check the imagePickerView outlet is connected.

答案评论:

Man, that’s about as beginner of a mistake as I could make. I do have one additional question. When I select the image, the modal isn’t dismissed. The image will display on screen after I select an image and the press Cancel. How do I skip that last step?
– Andrew Tuzson
4 hours ago
Also, is creating the reference to the delegate and assigning the delegate inside of the pickAnImage method a good idea? I’ve seen these declared at the beginning of the class and inside of methods. I’m unsure which is best practice.
– Andrew Tuzson
4 hours ago
Dismiss the picker when selected also. Delegate reference is kept by UIImagePickerController. We do not have to worry. But the idea is these assignment is not needed to be when the button is clicked, since assigning delegate can be done at the loading time. In button click only present the picker. (In this way you have to keep global reference for picker)

答案3:

  • Ensure you have made all inclusions in the info.plist file
  • Ensure you have all the delegates set
  • Ensure what you are using to display image (e.g if editing is set to true, then UIImagePickerControllerEditedImage else UIImagePickerControllerOriginalImage)

答案评论:

答案4:

import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet weak var imagePickerView: UIImageView!


override func viewDidLoad() {
    super.viewDidLoad()
}


func btnPickImageAction(_ sender: UIButton) {

    // To Get image from photoLibrary source type
    if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = .photoLibrary;
        imagePicker.allowsEditing = true
        self.present(imagePicker, animated: true, completion: nil)
    }

    /*
    // To capture image from camera Source Type
    if UIImagePickerController.isSourceTypeAvailable(.camera) {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = .camera;
        imagePicker.allowsEditing = false
        self.present(imagePicker, animated: true, completion: nil)
    }
    */
}

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

}

答案评论:

Don’t forgot to add Privacy – Photo Library Usage Description and Privacy – Camera Usage Description to your info.plist file

原文地址:

https://stackoverflow.com/questions/47752434/displaying-selected-image-on-screen-using-uiiamgepickercontrollerdelegate-in-swi

添加评论

友情链接:蝴蝶教程