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 Build a Diary App Using Core Data Custom Detail View Controller Implementing a UIImagePickerController

Scott Muhs
Scott Muhs
2,621 Points

SourceType for image picker doesn't allow both PhotoLibrary or Camera to be called from ActionSheet

I have used this code for the imagePicker in one of my apps. I have updated it to be used with the Swift language. However, What I'm noticing is that when my action sheet pops up, it gives me two options (camera or photo roll). However, If I click on PhotoRoll it takes me to the library, if i click on camera it also takes me to the library. I checked out the code and the IF/Else statement calls both. Below is the code. Any thoughts on why the Camera isn't being called?

func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        dismissViewControllerAnimated(true, completion: nil)
        scrollView.setContentOffset(CGPointMake(0, -65), animated: true)
    }

    func promptForSource(){
        let actionSheet = UIActionSheet(title: "Image Source", delegate: self, cancelButtonTitle: "Cancel", destructiveButtonTitle: nil, otherButtonTitles: "Camera", "Photo Roll")

        actionSheet.showInView(self.view)

    }

    func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
        if buttonIndex != actionSheet.cancelButtonIndex {

            if buttonIndex != actionSheet.firstOtherButtonIndex {
                promptForPhotoRoll()

            }
            else{
                promptForCamera()
            }

        }
    }

    func promptForCamera(){
        let controller = UIImagePickerController()
        controller.sourceType = UIImagePickerControllerSourceType.Camera
        controller.delegate = self
        presentViewController(controller, animated: true, completion: nil)
    }

    func promptForPhotoRoll(){
        let controller = UIImagePickerController()
        controller.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        controller.delegate = self
        presentViewController(controller, animated: true, completion: nil)
    }


    @IBAction func p1PhotoTapped(sender: AnyObject) {
        buttonToSet = .Button1

        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){
            promptForSource()



        }else{
            promptForPhotoRoll()
        }


    }