UIImagePickerController in iOS

In this tutorial ,  I will tell you about UIImagePickerController in iOS. UIImagePicker are used to pick images either from CameraRoll or from Camera Application.






Step 1 :   Create a single view application.

Step 2 :   Drag two buttons and a imageView on the storyboard.





and add constraints of UIImageView and UIButton .

Step 3 :    Make connection with UIViewController .



Step 4 :    Add UIButton`s Action to the UIViewController 



Step 5:    In ViewController.h ,  add imagePicker delegate and UINavigationControllerDelegate

@interface ViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate>


Step 6:  In ViewController.m , add the UIImagePicker`s on the button`s action as follows : 

- (IBAction)choosePhotoAction:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
- (IBAction)takePhotoAction:(id)sender {
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:@"Error"
message:@"No camera found"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"OK", @"OK action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(@"OK action");
}];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
}
else
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
}
view raw ImagePicker hosted with ❤ by GitHub


Here , on taking picture from camera ,firstly we are checking whether device has camera or not. 



Step 7:   Implementing UIImagePickerDelegates :

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.myImageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}



Step 8:  Build and run the app .



Your camera application is been created in which you can take photos from camera roll or click photos from camera . 


Thank you .   :)


UIPickerView With Done button











Comments

  1. Thanks for this beautiful post IOS. It is really good for the beginners and I also liked the post.
    IOS Training in Bangalore
    IOS Training in Marathahalli

    ReplyDelete

Post a Comment

Popular Posts