Take Image from Camera or Library in Objective c.


Step 1. In .sib file

1. Add an image view in it

2. Add Button .

Step 2.  In .h file Paste Following Code.

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<uiimagepickercontrollerdelegate,uinavigationcontrollerdelegate,uiactionsheetdelegate>{

IBOutlet UIImageView  *img;

IBOutlet UIButton *processButton;

IBOutlet UIButton *takeImage;

}

-(IBAction)processIt:(int)val;

-(IBAction)showActionSheet:(id)sender;

@end

********* Don’t Forget to Add Delegates**********

Step 3. Add Following Method For ActionSheet.

-(IBAction)showActionSheet:(id)sender {

UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:@”Take Image” delegate:self cancelButtonTitle:@”Cancel Button” destructiveButtonTitle:@”Destructive Button” otherButtonTitles:@”From Camera”, @”From Library”, nil];

popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;

[popupQuery showInView:self.view];

[popupQuery release];

}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

if (buttonIndex == 0) {

} else if (buttonIndex == 1) {

[self TakeImage:1];

} else if (buttonIndex == 2) {

[self TakeImage:2];

} else if (buttonIndex == 3) {

}

************** From Action sheet Method We calling  [self TakeImage:1]; *********

Step 4. Add TakeImage Method .

-(void) TakeImage:(int)val{

UIImagePickerController * picker = [[UIImagePickerController alloc] init];

picker.delegate = self;

picker.allowsEditing = NO;

if(val ==2) {

picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

[self presentModalViewController:picker animated:YES];

} else {

if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@”Error”

message:@”Device has no camera”

delegate:nil

cancelButtonTitle:@”OK”

otherButtonTitles: nil];

[myAlertView show];

}else{

picker.sourceType = UIImagePickerControllerSourceTypeCamera;

[self presentModalViewController:picker animated:YES];

}

}

}

Step 5 . Add ImageController Method.

– (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo

{

[picker dismissModalViewControllerAnimated:YES];

UIImage *myImage = image;

[img setImage:myImage];

}

*********   img is name of imageview you can change as your requirement.   ************

BEST OF LUCK

Image

Advertisements

One thought on “Take Image from Camera or Library in Objective c.

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: