Hiii guys,
Today I'll be demonstrating how to pick any image via Gallery or via Camera in iOS ObjecticeC
1. Create a ViewController & add these two delegates to it-UIImagePickerControllerDelegate and UINavigationControllerDelegate.
2. Now add two buttons one for accessing Camera & another for Gallery.
3. Make their actions as- - (IBAction)takePhoto:(id)sender; - (IBAction)selectPhoto:(id)sender;
4. Add the following code to the action of both the buttons as-
//Takephoto via camera//
- (IBAction)takePhoto:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = self; picker.allowsEditing = YES;picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
//SelectPhoto via gallery//
- (IBAction)selectPhoto:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
5. Take another ViewController & add an ImageView to it & make an IBOutlet of this ImageView,name this ViewController as SecondView Controller.
6. Add UIImagePickerControllerDelegate and UINavigationControllerDelegate delegates in secondView Controller
7. Now create a push segue from Gallery button to the SecondView Controller
8. After adding imagepicker delegate, add this function to seconView Controller
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imgView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Here imgView is the imageView that we have created. Thanks a lot for reading, I hope it helps.
0 Comment(s)