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
Jack Ryder
7,286 PointsHow to display an image taken from the camera in an imageView
Just wondering how i would go about using the image taken by the camera to display that image in an imageView
thanks!
1 Answer
Stone Preston
42,016 Pointsbasically you would need to use a UIImagePicker and its delegate methods to do this. this video covers adding the code below
you need to conform to the UIImagePickerControllerDelegate and UINavigationControllerDelegate protocols in your .h
@interface SomeViewController : UITableViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
and also add a property for a imagePickerController and a property to hold the image that gets taken
@property (nonatomic, strong) UIImagePickerController *imagePicker;
@property (nonatomic, strong) UIImage *image;
then whenever you want the imagePicker to appear present the imagePicker
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
self.imagePicker.allowsEditing = NO;
//camera is available
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
} else {
self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];
[self presentViewController:self.imagePicker animated:NO completion:nil];
then implement a few delegate methods:
#pragma mark - ImagePickerController Delegate
//handles cancelation
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:NO completion:nil];
}
//this runs when you finish taking a picture
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
//set the photo that was taken as your photo property
self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
//set your imageViews image to the image property
self.imageView.image = self.image
if(self.imagePicker.sourceType == UIImagePickerControllerSourceTypeCamera) {
//save the image to the photos album
UIImageWriteToSavedPhotosAlbum(self.image, nil, nil, nil);
}
}
[self dismissViewControllerAnimated:YES completion:nil];
}