Lance Samaria
Lance Samaria

Reputation: 19572

iOS Camera with multiple imageView outputs

In my FirstVC I understand how to work with the UIImagePickerController, load the camera, take a photo, display it inside 1 imageView (still within the FirstVC) and then pass that image to an imageView inside the SecondVC via prepareForSegue to be shown over there.

What I need help with is having multiple imageViews inside FirstVC so that once a user takes the first picture it loads into the 1st imageView, then second picture the 2nd imageView, 3 pic to 3rd imageView, and 4th pic to 4th imageView. Afterwards an alert would appear that would say the user can't take any more pics and the user would press a nextSceneButton to be taken be taken to the SecondVC which would display the 4 chosen images that they just took.

I always used UIImagePickerController but I just got acquainted with AVFoundation. Should I use AVFoundation or UIImagePicker for this task?

Here is my UIImagePickerController code with only one imageView inside the FirstVC:

import UIKit

class FirstViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var nextSceneButton: UIButton!
//nextSceneButton is connected in StoryBoards and triggers the segue to the SecondVC


let imagePicker = UIImagePickerController()

var image: UIImage?



  //MARK:- Custom Function
func noCamera(){
        let alertVC = UIAlertController(title: "No Camera", message: "Sorry, this device has no camera", preferredStyle: .Alert)
        let okAction = UIAlertAction(title: "OK", style:.Default, handler: nil)
        alertVC.addAction(okAction)
        presentViewController(alertVC, animated: true, completion: nil)
    }


override func viewDidLoad() {
        super.viewDidLoad()
        imagePicker.delegate = self      
    }


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

  //MARK:- Buttons

@IBAction func libraryButtonPressed(sender: UIButton) {
        imagePicker.allowsEditing = false
        imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        presentViewController(imagePicker, animated: true, completion: nil)
    }


@IBAction func photoButtonPressed(sender: UIButton) {

    if (UIImagePickerController.availableCaptureModesForCameraDevice(UIImagePickerControllerCameraDevice.Rear) != nil){
            imagePicker.allowsEditing = false
            imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
            imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Photo
            imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.Rear
            presentViewController(imagePicker, animated: true, completion: nil)
        }else{
                noCamera()
    }
     }


  //MARK:- ImagePickerController Delegates

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
            let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
            self.image = chosenImage
            imageView.image = self.image
            dismissViewControllerAnimated(true, completion: nil)
     }


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


  //MARK:- Segue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == “fromPhotoVcToSecondVC” {
        let secondVC = segue.destinationViewController as! SecondViewController
        secondVC.image = self.image
        }
     }

}//end class

Upvotes: 1

Views: 1442

Answers (1)

stefos
stefos

Reputation: 1235

You can add a count property and hold your images in an array :

var count = 0
var imageViews:[UIImageView]

Every time you take a photo and delegate gets called load the respective imageview :

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
        self.imageViews[count].image = chosenImage
        count +=1
        dismissViewControllerAnimated(true, completion: nil)
 }

Then if all photos are taken alert the user :

@IBAction func photoButtonPressed(sender: UIButton) {
      if count < imageViews.count {
          //present UiimagePicker
      } else {
          // alert no more pictures allowed
      }
}

Upvotes: 1

Related Questions