Example With Camera Button In Top Navigation Bar
In #ViewController.h
@interface #ViewController : UIViewController
{
In #ViewController.m
//********** INIT **********
- (id)init
{
[super initWithNibName:@"ItemDetailViewController" bundle:nil];
//----- ADD CAMERA BUTTON TO NAVIGATION BAR -----
UIBarButtonItem *cameraBarButtonItem =
[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
target:self
action:@selector(takePicture:)];
[[self navigationItem] setRightBarButtonItem:cameraBarButtonItem]; //Add to nav bar when this view controller is on the top of the stack
[cameraBarButtonItem release]; //It is retained by the navitation item so we can release
return self;
}
//********** TAKE PICTURE **********
-(void)takePicture:(id)sender
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
//Use camera if device has one otherwise use photo library
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
}
else
{
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
[imagePicker setDelegate:self];
//Show image picker
[self presentModalViewController:imagePicker animated:YES];
//Modal so we wait for it to complete
[imagePicker release];
}
//********** RECEIVE PICTURE **********
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//Get image
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
//Display in ImageView object (if you want to display it
[imageView setImage:image];
//Take image picker off the screen (required)
[self dismissModalViewControllerAnimated:YES];
}