Use Camera and photo Library in iPhone

The iPhone has a built-in camera and nifty application called photos to help you to manage all those awesome pictures you’ve taken . Your programs can use the built-in camera to take pictures and your your program can also allow the user to select picture from among the photos already on the iPhone. The both camera and image library are made available to your application by way of an image picker.

The iPhone has  a built-in camera and nifty application called photos to help you to manage all those awesome pictures you’ve taken . Your programs can use the built-in camera to take pictures and your your program can also allow the user to select picture from among the photos already on the iPhone. The both camera and image library are made available to your application by way of an image picker.

Step 1: Create a new project in Xcode using the view base application and naming the application.

Step 2:Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: Single click ViewController.h file and add UIButton, UIImage ,IBAction also. Make the following changes into the file.

#import <UIKit/UIKit.h>

@interface CameraViewController : UIViewController
< UIImagePickerControllerDelegate , UINavigationControllerDelegate>
{
        IBOutlet UIImageView *imageView;
        IBOutlet UIButton *takePictureButton;
        IBOutlet UIButton *selectFromCameraRollButton;
}

@property(nonatomic,retain) UIImageView *imageView;
@property(nonatomic,retain) UIButton *takePictureButton;
@property(nonatomic,retain) UIButton *selectFromCameraRollButton;

-(IBAction)getCameraPicture:(id)sender;
-(IBAction)selectExitingPicture;

Step 4: Double click .xib file, open it to the Interface Builder. Drag two Round Rect button from the library and place it bottom of the view window. Give it a title of Camera and Library. Next drag image view from the library, and place it above the buttons. Now connect the File’s Owner icon to the image view and select imageView. Single click camera button and bring up connection inspector . Drag Touch Up Inside event to File’s Owner, and select the getCameraPicture: action. Next select Library button and bring up connection inspector . Drag Touch Up Inside event to File’s Owner, and select the selectExitingPicture: action. Now Save your .xib file and go back to the  Xcode.

Step 5: Single click your ViewController.m file . Add two methods and two delegate methods. Make the following changes.

-(IBAction)getCameraPicture:(id)sender
{
 UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  picker.delegate = self;
  picker.allowsImageEditing = YES;
  picker.sourceType = (sender == takePictureButton) ?    UIImagePickerControllerSourceTypeCamera :
 UIImagePickerControllerSourceTypeSavedPhotosAlbum;
  [self presentModalViewController: picker animated:YES];
  [picker release];
}

-(IBAction)selectExitingPicture
{
  if([UIImagePickerController isSourceTypeAvailable:
  UIImagePickerControllerSourceTypePhotoLibrary])
{
  UIImagePickerController *picker= [[UIImagePickerController alloc]init];
  picker.delegate = self;
  picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
   [self presentModalViewController:picker animated:YES];
   [picker release];
}
}

-(void)imagePickerController:(UIImagePickerController *)picker
  didFinishPickingImage : (UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
  imageView.image = image;
  [picker dismissModalViewControllerAnimated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)  picker
{
  [picker dismissModalViewControllerAnimated:YES];
}

Step 6: Now compile your application run into the simulator. But you do not have the option to take a new photo. If you have the opportunity to run the on a real device, go ahead and try it. 

Figure 1: This is the Library view.

You can downloaded SourceCode from here Camera

WP Greet Box icon
Hello there! If you are new here, you might want to subscribe to the RSS feed for updates on this topic, and free programming tips and tricks and source code            snippets.

2 Responses to “Use Camera and photo Library in iPhone”

  1. Jack says:

    hi,
    how would i make it so the photo libary shows up in a splitview in the iPad?

Leave a Reply

Security Code:

learn iphone programming

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Our Mobile Training Courses

EDUmobile.ORG offers the following 4 Mobile Training Courses. Our iPhone Training Course is very popular, with over 200 developers in training.

learn iphone programming
© 2010 EDUmobile.ORG