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

Camera View Controller

What code do I need to implement to allow the user to have the option to select a picture from their camera roll. Instead of having to take a picture.

5 Answers

WIthout implementing a custom camera interface, id say the simplest thing to do would be to use an action sheet.

you can use an action sheet to present two choices, take photo or choose from photo library. to do this you need to conform to the action sheet delegate protocol

@interface SomeViewController : UIViewController<UIActionSheetDelegate>

then whenever you want to present the action sheet

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Take Photo", @"Choose from library", nil];

//set the actionsheet delegate
 actionSheet.delegate = self;

//show the action sheet from tab bar
[actionSheet showFromTabBar:self.tabBarController.tabBar];

and implement the following delegate method

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

    //user clicked take photo
    if (buttonIndex == 0 ) {

       //change your image pickers source type to camera

       self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

        [self presentViewController:self.imagePicker animated:NO completion:nil];


    }

     //user clicked choose from library
     if (buttonIndex == 1) {

            //change your image pickers source type to photo library

      self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        [self presentViewController:self.imagePicker animated:NO completion:nil];

   }
}

The action sheet works, but when I click on one of the buttons the app crashes

what error are you getting?

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target <FriendsViewController: 0x14e640b0>.'

make sure you set up the imagePicker property in viewDidLoad. sounds like it didnt get initialized or something

How would I set that up in the viewDidLoad?

self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
self.imagePicker.allowsEditing = NO;

something along those lines.

thank you! works great

awesome. glad you got it working

I have another question. After i am done selecting an image from the the photo library, I want it to transfer over to my recipients view controller What code would I need to implement to get to do that?