0

I have 1 UIImageView, user can tap on UIImageView to select photo from photo library,

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(cameraTapped(tapGestureRecognizer:)))           
cameraUIView.isUserInteractionEnabled = true
cameraUIView.addGestureRecognizer(tapGestureRecognizer)

where cameraTapped is

func cameraTapped(tapGestureRecognizer: UITapGestureRecognizer)
    {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        actionSheet.addAction(UIAlertAction(title: "写真を撮る", style: .default, handler: { (action:UIAlertAction) in
            if UIImagePickerController.isSourceTypeAvailable(.camera) {
                imagePicker.sourceType = .camera
                imagePicker.allowsEditing = true

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

        }))

        actionSheet.addAction(UIAlertAction(title: "アルバムから選択する", style: .default, handler: { (action:UIAlertAction) in
            if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
                imagePicker.sourceType = .photoLibrary
                self.present(imagePicker,animated: true,completion: nil)
            }

        }))


        actionSheet.addAction(UIAlertAction(title: "キャンセル", style: .cancel, handler: nil))

        self.present(actionSheet, animated: true, completion: nil)

and

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
            selectedImage = image
            cameraUIImageView.image = image
            cameraUIImageView.contentMode = .scaleAspectFill
            cameraUIImageView.clipsToBounds = true

            let leadingConstrain = NSLayoutConstraint(item: cameraUIImageView, attribute: .leading, relatedBy: .equal, toItem: cameraUIImageView.superview, attribute: .leading, multiplier: 1, constant: 0)
            leadingConstrain.isActive = true

            let trailingConstrain = NSLayoutConstraint(item: cameraUIImageView, attribute: .trailing, relatedBy: .equal, toItem: cameraUIImageView.superview, attribute: .trailing, multiplier: 1, constant: 0)
            trailingConstrain.isActive = true

            let topConstrain = NSLayoutConstraint(item: cameraUIImageView, attribute: .top, relatedBy: .equal, toItem: cameraUIImageView.superview, attribute: .top, multiplier: 1, constant: 0)
            topConstrain.isActive = true

            let bottomConstrain = NSLayoutConstraint(item: cameraUIImageView, attribute: .bottom, relatedBy: .equal, toItem: cameraUIImageView.superview, attribute: .bottom, multiplier: 1, constant: 0)
            bottomConstrain.isActive = true


        }
        dismiss(animated: true, completion: nil)
        cameraLabel.text = ""

    }

This works. However, I want to have 3 UIImageView, tap on each of UIImageView will let user select photo and display that photo on UIImageVIew.

Do I need to create 3 different imagePickerController? And how to do that?, because imagePickerController the implementation of UIImagePickerDelegate.

enter image description here

rmaddy
  • 314,917
  • 42
  • 532
  • 579
John
  • 3,888
  • 11
  • 46
  • 84

4 Answers4

1

You can add all UIImageView as subview of main view. Add UITapGestureRecognizer to main view. When user taps the main view copy the id of tapped imageview and use it to show image.

var activeImageView:UIImageView? 

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(cameraTapped(tapGestureRecognizer:)))           
mainView.isUserInteractionEnabled = true // mainView is super view of all 3 imageViews.
mainView.addGestureRecognizer(tapGestureRecognizer)

You can get your tapped imageView from hitTest: method as

func cameraTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
  let view = tapGestureRecognizer.view
  let loc = tapGestureRecognizer.location(in: view)
  activeImageView = view?.hitTest(loc, with: nil) as? UIImageView
  //.... your code
}

and set image to your active image view as

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
            selectedImage = image
            activeImageView.image = image
            //.... your code
        }
}

Do let me know if you have any queries in comment, I will try and help.

luckyShubhra
  • 2,731
  • 1
  • 12
  • 19
0

you only need one imagePickerController, if using collectionView, you should save the indexPath that user tap, if using 3 UIImageView, you should save id of uiimageView and set image to it after user chooses.

Hope this help you

tai do
  • 124
  • 6
0

ImagePickerController give you image from photo library, it's good. like this: if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {

But ImagePickerController did not know what the image was doing. You should specify which imageView is going to display the image.

  1. declare a currentTappedImageView

var currentTappedImageView: UIImageView?

  1. save imageView when you tap

currentTappedImageView = tapGestureRecognizer.view as? UIImageView

  1. display the image selected from ImagePickerController to on currentTappedImageView

currentTappedImageView.image = selectedImage

Codus
  • 1,433
  • 1
  • 14
  • 18
  • Thanks. I got the idea. When I implement it, I face another problem. I need to addGestureRecognizer to all UIImageView. When I did like below, I can only tap the last UIImageView, which is plus1UIImageView. Do you know why? Thanks -------- cameraUIImageView.isUserInteractionEnabled = true cameraUIImageView.addGestureRecognizer(tapGestureRecognizer) plus1UIImageView.isUserInteractionEnabled = true plus1UIImageView.addGestureRecognizer(tapGestureRecognizer) – John Aug 16 '17 at 02:50
  • You need multiple `tapGestureRecognizer`. But all `tapGestureRecognizer` can use the same callback(selector). – Codus Aug 16 '17 at 03:09
0

You only need a one instance of UIImagePickerviewController. But you have to track which image view you fill.