Capturing or Browsing photo Programmatically in iPhone

Capturing or Browsing photo Programmatically in iPhone

Using UIImagePicker Photo Browsing in Application is possible.


UIImageView *empImage = [[UIImageView alloc] init];
UIImage *im = [UIImage imageNamed:@"ImagePlaceholder.png"];
[empImage setImage:im];
empImage.frame = CGRectMake(60, 40, 200, 200);
[self.view addSubview:empImage];

//Choose Image button
UIButton *showImage = [[UIButton alloc]init];
UIImage *Image = [UIImage imageNamed:@"browseButton.png"];
[showImage setBackgroundImage:Image forState:UIControlStateNormal];
[showImage addTarget:self action:@selector(getPhoto:)
showImage.frame = CGRectMake(62,250,96,33); forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:showImage];

//Caputure Image button
UIButton *captureImage = [[UIButton alloc]init];
UIImage *Image1 = [UIImage imageNamed:@"captureButton.png"];
[captureImage setBackgroundImage:Image1 forState:UIControlStateNormal];
[captureImage addTarget:self action:@selector(capturePicture:) forControlEvents:UIControlEventTouchUpInside];
captureImage.frame = CGRectMake(162,250,96,33);
[self.view addSubview:captureImage];

PlaceHolder for Image and browse


Browse button opens the phone gallery where Images are saved.

// method for “showImage” button
-(IBAction) getPhoto:(id) sender {
UIImagePickerController * picker = [[[UIImagePickerController alloc] init]autorelease];
picker.delegate = self;

if((UIButton *) sender == choosePhotoBtn) {
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
} else {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}

[self presentModalViewController:picker animated:YES];
}

Browse Image


// method for “captureImage” button
-(IBAction)capturePicture:(id)sender{

if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Message" message:@"Unable to Capture image" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
[alert release];
}else{
// if capture image is not supported by the device or Stimaulator
UIImagePickerController* imagePickerController = [[[UIImagePickerController alloc]init]autorelease];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.delegate = self;
imagePickerController.allowsEditing = YES;
[self presentModalViewController:imagePickerController animated:YES];
}
}

Capture Button opens iPhone Camera and Stores in Application Directory

// delegate imagePickerController
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
}

The UIImagePickerController class manages customizable, system-supplied user interfaces for taking pictures and movies on supported devices, and for choosing saved images and movies for use in your app. An image picker controller manages user interactions and delivers the results of those interactions to a delegate object.

Delegates to be included in .h file : <UIImagePickerControllerDelegate,UIPopoverControllerDelegate> Frameworks required in the Application : UIKit.framework, Foundation.framework, CoreGraphics.framework

Loaded Image

0 comments:

Post a Comment