Steps
to Capture Image Using Camera/Photo Gallery (iOS)
In
this tutorial I will describe how to capture image, using iPhone/iPad Device
Camera or Photo Gallery, step by step. Using this tutorial you can
implement image capturing functionality within 1 hour.
Step-1:
In your user interface create an UIImageView and two UIButton and
place them on interface as per your requirement. Image view to show
captured image, one button to open camera and second button to open
image gallery.
Step-2:
Confirm to required protocols in your controller .h file
@interface YourViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
- (IBAction)takePhoto:(UIButton *)sender {
@interface YourViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
- (IBAction)takePhoto:(UIButton *)sender {
Step-3:
Declare an IBOutlet for image in .m file.
@interface
YourViewController : UIViewController
@property
(strong, nonatomic) IBOutlet
UIImageView
*imageView;
@end
Step-4:
Define an IBAction takePhoto method as below:
UIImagePickerController
*picker = [[UIImagePickerController
alloc]
init];
picker.delegate
= self;
picker.allowsEditing
= YES;
picker.sourceType
= UIImagePickerControllerSourceTypeCamera;
[self
presentViewController:picker animated:YES
completion:NULL];
}
Step-5:
Define selectPhoto method as below:
-
(IBAction)selectPhoto:(UIButton
*)sender {
UIImagePickerController
*picker = [[UIImagePickerController
alloc]
init];
picker.delegate
= self;
picker.allowsEditing
= YES;
picker.sourceType
= UIImagePickerControllerSourceTypePhotoLibrary;
[self
presentViewController:picker animated:YES
completion:NULL];
}
Step-6:
In interface builder connect UIImageView with imageView property.
Connect buttons 'Touch up inside' action with takePhoto and
selectPhoto methods.
Step-7: To handle UIImagePickerControllerDelegate, implement below two methods.
/*This
method will be called when user capture/select an image*/
-
(void)imagePickerController:(UIImagePickerController
*)picker didFinishPickingMediaWithInfo:(NSDictionary
*)info {
UIImage
*chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image
= chosenImage;
[picker
dismissViewControllerAnimated:YES
completion:NULL];
}
/*This
method will be called when user perform cancel operation*/
-
(void)imagePickerControllerDidCancel:(UIImagePickerController
*)picker {
[picker
dismissViewControllerAnimated:YES
completion:NULL];
}
Enjoy
learning and please let me know your feedback.
No comments:
Post a Comment