Adding images from cell phones is a very important task in Mobile Application development. Social media, Bussiness applications, Real estate applications, Lifestyle applications, E-commerce applications. We would require getting images from users where we are using “UIImagePickerControllerDelegate” Delegates. UIImagePickerControllerDelegate has a set of functions by using this delegate objects are able to interact with image picker interface. It inherits from NSObjectProtocol.
For getting additional information about the picked object it may be image or video, UIImagePickerControllerDelegate has provided some predefined keys for a delegate object to know what exactly returned. Like ImagePickerControllerMediaType to specify that type of media returned from delegate object. ImagePickerControllerOriginalImage specifies that uncropped and original image. ImagePickerControllerImageUrl represent path/URL of returned image. ImagePickerControllerMediaMetaData returns information regarding to newly captured image.
ImagePickerControllerEditedImage specifies that the image is edited by user or not. ImagePickerControllerEditedImage presenting selected or captured image. Follow the below steps to pick an image from the gallery or capture it from the camera.
1) Create a new project imagePickerDemo.
2) Add NSPhotoLibraryUsageDescription into info.plist
3) Create a button and add action to it.
4) Add imageView in ViewController and Add outlet of it.
5) Add UIImagePickerControllerDelegate. Create one variable for UIImagePickerController.And image for storing image temporary purpose
code:
1 2 3 4 5 6 7 8 |
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UIImagePickerControllerDelegate> { UIImagePickerController *imgPickerCObj; UIImage *image; } - (IBAction)btnAddPhoto:(id)sender; @property (weak, nonatomic) IBOutlet UIImageView *imgUpload; @end |
6) Add UIActionSheet when user clicking button action method.
code:
1 2 3 4 |
- (IBAction)btnAddPhoto:(id)sender { UIActionSheet *callactionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:(id)self cancelButtonTitle:NSLocalizedString(@"Cancel",nil) destructiveButtonTitle:nil otherButtonTitles:NSLocalizedString(@"Take photo",nil), NSLocalizedString(@"Choose from gallery",nil), nil]; [callactionSheet showInView:self.view]; } |
7)Add UIActionSheet method clickedButtonAtIndex
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex== 0) { [[NSOperationQueue mainQueue] addOperationWithBlock:^{ imgPickerCObj = [[UIImagePickerController alloc] init]; imgPickerCObj.sourceType = UIImagePickerControllerSourceTypeCamera; [imgPickerCObj setDelegate:(id)self]; imgPickerCObj.allowsEditing = YES; [self presentViewController:imgPickerCObj animated:YES completion:nil]; }]; } else if (buttonIndex== 1) { [[NSOperationQueue mainQueue] addOperationWithBlock:^{ imgPickerCObj = [[UIImagePickerController alloc] init]; imgPickerCObj.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [imgPickerCObj setDelegate:(id)self]; [self presentViewController:imgPickerCObj animated:YES completion:nil]; imgPickerCObj.allowsEditing = YES; imgPickerCObj.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypePhotoLibrary]; }]; } } |
8) Add imagepickerDelegate for didFinishPickingMediaWithInfo
Code:
1 2 3 4 5 |
-(void) imagePickerController:(UIImagePickerController *) picker didFinishPickingMediaWithInfo:(NSDictionary *) info { image = [info objectForKey:UIImagePickerControllerEditedImage]; self.imgUpload.image = image; [self.navigationController dismissViewControllerAnimated: YES completion: nil]; } |
Now see the Output Thank you. Please comment and share the article if you find something useful. Happy Coding.
very nice article thanks…
Thank you.
This is very useful information for me. thank you.
Thank you.