Search code examples
swiftxcodeuiimagepickercontroller

How to modify imagePickerController (source camera) not to dismiss after pressing "Use photo" until 4 photos are taken?


I want to take 4 photos before UIImagePickerController is dismissed. How to modify it? I have following button action and delegate:

@IBAction func take4Photos(_ sender: Any) {
                if UIImagePickerController.isSourceTypeAvailable(.camera) {
                    let image = UIImagePickerController()
                    image.delegate = self
                    image.sourceType = .camera;
                    image.allowsEditing = false
                    self.present(image, animated: true, completion: nil)
                    
                }
            }


func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage{
       
        imagePicked = image
       
        self.dismiss(animated: true, completion: nil)
        
    }

Solution

  • I will recommend you to use ImagePicker, which can be easily installed through cocoaPods and it can fulfill your requirement with just one line of Code.

    let imagePickerController = ImagePickerController()
    imagePickerController.imageLimit = 4
    

    You can check ImagePicker library here

    Here's how you can use ImagePicker library to pick multiple images.

    Create a button in Storyboard and have its outlet in the ViewController.

    @IBOutlet weak var chooseImage: UIButton!
    

    If you want to see the selected images in VC after selection, make an array of UIImages like this

    var imageViews:[UIImageView] = []
    

    Then, in viewDidLoad, add target to chooseImage button

    chooseImage.addTarget(self, action: #selector(buttonTouched(button:)), for: .touchUpInside)
    

    And declare buttonTouched function outside the viewDidLoad like this

      @objc func buttonTouched(button: UIButton) {
      let config = Configuration()
      config.doneButtonTitle = "Done"
      config.noImagesTitle = "Sorry! There are no images here!"
      config.recordLocation = false 
      config.allowVideoSelection = false. //If you don't want video recording
    
      let imagePicker = ImagePickerController(configuration: config)
      imagePicker.delegate = self
    
      present(imagePicker, animated: true, completion: nil)
    }
    

    Then, in the end, inside the extension, conform to ImagePicker delegate functions like this.

    extension OwnerAddListingFacilitiesViewController:ImagePickerDelegate {
    
     func cancelButtonDidPress(_ imagePicker: ImagePickerController) {
        
        imagePicker.dismiss(animated: true, completion: nil)
      }
    
    
     func wrapperDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) { //Don't know what exactly this function does }
    
     func doneButtonDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) {
        for Count in 0..<images.count {
            let imageView = UIImageView(image: images[Count])
            imageView.frame = CGRect(x: (0 * (110 * Count)), y: 0, width: 50, height: 50)
            imageViews.append(imageView)
        }
        
            imagePicker.dismiss(animated: true, completion: nil)
      }
    
     }
    

    You'll be able to see small thumbnail like images after selecting them and pressing done.