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
James Atlee
203 PointsCamera 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
Stone Preston
42,016 PointsWIthout 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];
}
}
James Atlee
203 Points*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target <FriendsViewController: 0x14e640b0>.'
Stone Preston
42,016 Pointsmake sure you set up the imagePicker property in viewDidLoad. sounds like it didnt get initialized or something
James Atlee
203 PointsHow would I set that up in the viewDidLoad?
Stone Preston
42,016 Pointsself.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
self.imagePicker.allowsEditing = NO;
something along those lines.
James Atlee
203 Pointsthank you! works great
Stone Preston
42,016 Pointsawesome. glad you got it working
James Atlee
203 PointsI 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?
James Atlee
203 PointsJames Atlee
203 PointsThe action sheet works, but when I click on one of the buttons the app crashes
Stone Preston
42,016 PointsStone Preston
42,016 Pointswhat error are you getting?