iosswiftuiimageviewuiimagepickercontroller

Picking two different images in the same view controller using imagePickerController in Swift


I am making an app in which there are two UIImageViews. In each image view the user needs to be able to input a different image. Here is the code I have so far.

    var imagePicker = UIImagePickerController()
    @IBAction func chooseImage1(sender: AnyObject) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
        println("Button capture")


        imagePicker.delegate = self
        imagePicker.sourceType = .SavedPhotosAlbum
        imagePicker.allowsEditing = false

        self.presentViewController(imagePicker, animated: true, completion: nil)
    }
}

@IBAction func chooseImage2(sender: AnyObject) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
        println("Button capture")


        imagePicker2.delegate = self
        imagePicker2.sourceType = .SavedPhotosAlbum
        imagePicker2.allowsEditing = false

        self.presentViewController(imagePicker, animated: true, completion: nil)
}
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
    let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
        chooseImage1.image = pickedImage

    let pickedImage2 = info[UIImagePickerControllerOriginalImage] as? UIImage
    chooseImage2.image = pickedImage2



    dismissViewControllerAnimated(true, completion: nil)
}



func imagePickerControllerDidCancel(picker: UIImagePickerController) {
    dismissViewControllerAnimated(true, completion: nil)
}

This ends up picking the same image for each different image view. I would like to be able to choose two individual photos, one for each view. Thank you for your help.


Solution

  • You only need one UIImagePickerController. You can keep a reference of the tapped view and when the user finish picking the image, you just need to cast the selected view as UIImageView and set its image property:

    update: Xcode 11.5 • Swift 5.2 or later

    import UIKit
    
    class ViewController: UIViewController, UIImagePickerControllerDelegate ,UINavigationControllerDelegate {
        
        @IBOutlet weak var imageView1: UIImageView!
        @IBOutlet weak var imageView2: UIImageView!
    
        var imagePicker = UIImagePickerController()
        var selectedView: UIView!
     
        override func viewDidLoad() {
            super.viewDidLoad()
            
            imagePicker.delegate = self
            imagePicker.sourceType = .savedPhotosAlbum
            imagePicker.allowsEditing = false
            
            [imageView1,imageView2].forEach {
                $0?.isUserInteractionEnabled = true
                $0?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(chooseImage)))
            }
        }
        
        @objc func chooseImage(_ gesture: UITapGestureRecognizer) {
            if UIImagePickerController.isSourceTypeAvailable(.savedPhotosAlbum) {
                selectedView = gesture.view
                present(imagePicker, animated: true)
            }
        }
        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
            (selectedView as? UIImageView)?.image = info[.originalImage] as? UIImage
            dismiss(animated: true)
        }
        
        func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
            dismiss(animated: true)
        }
    }