Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Navid Mirzaie Milani
Navid Mirzaie Milani
6,274 Points

How to call the UIImagePickerControllerSourceTypeCamera continuously

I have the following problem when developing on iOS. I want to achieve that when I'm clicking on a tabbar item the camera shows up. Now that isn't so hard but the problem is this happens once. So when I click on another tabbar item and click back to the camera item, the camera doesn't show up again.

I think this is because when you click on cancel the UIImagePickerController dismisses and doesn't initialize again. How can I solve this problem, that when I'm clicking on the tabbar item the camara will always show up.

Thanks in advanced.

My UIImagePickerController does look as following.

class CameraViewController: UIViewController, UIImagePickerControllerDelegate,UINavigationControllerDelegate {

@IBOutlet weak var imageView: UIImageView!
let imagePicker:UIImagePickerController!

override func viewDidLoad() {
    super.viewDidLoad();
    self.loadCamera();
}


func loadCamera(){

        if UIImagePickerController.isSourceTypeAvailable(
            UIImagePickerControllerSourceType.Camera) {

                imagePicker = UIImagePickerController();
                imagePicker.delegate = self;
                imagePicker.sourceType = .Camera;

                presentViewController(imagePicker, animated: true, completion: nil)

        }
    }

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
        imagePicker.dismissViewControllerAnimated(true, completion: nil)
        imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage


    }


}

1 Answer

Hey Navid,

Try calling self.loadCamera() in viewDidAppear instead of viewDidLoad. This will ensure that code is called every time the view comes into play, rather then the first time it's loaded.

Navid Mirzaie Milani
Navid Mirzaie Milani
6,274 Points

@Jason,

Tnx but thats not the solution because now it repeat calling it. What i want is once and when i hit onother uitabbar item and go back to camera then it hits again. Now it does this only once. Tnx for helping me out. Just like an app as instagram. U want to take picture use the camera, then when you done you visit another tab. When you click once again at the cam it shows up again. In my case it shows up once (the first time) after that i doesn't show up again.