I just want the user to take a photo in app→crop→save. So I would want to use the allowsEditing property of UIImagePickerControl. However I don't know how i can do it. this is my code until now for camera.
-(IBAction)TakePhoto {
picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:picker animated:YES completion:NULL];
}
Use this for pick photo from camera
- (IBAction)takePhoto:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
use this for pick photo from photo library
- (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];
}
use following two delegate methods
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Add this in your ViewDidLoad method, so if your device is not support camera then display this alert message.
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Device has no camera"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[myAlertView show];
}
Add both protocols to the AppViewController.h file:
#interface APPViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
Related
Code in A_UIViewController:
PrestBViewController *aviewcontroller = [[PrestBViewController alloc] init];
[self presentViewController:aviewcontroller animated:YES completion:nil];
Code in B_UIViewController:
-(void)presentAction {
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = (id)self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imagePickerController animated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
When I run the code, it crashes at:
[picker.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
Can anyone help?
Code in A_viewcontroller
- (IBAction)PresentAction:(id)sender {
B_ViewController *aviewcontroller = [self.storyboard instantiateViewControllerWithIdentifier:#"aa"];
[self presentViewController:aviewcontroller animated:YES completion:nil];
}
Code in B_viewController
#implementation ViewController{
UIImagePickerController *picker;
}
- (void)viewDidLoad {
[super viewDidLoad]; //Check if Camera Available
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Error" message:#"Device has no Camera" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:ok];
[self presentViewController:alertController animated:YES completion:nil];
}
- (IBAction)takePic:(id)sender { //Button to get image from camera
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
- (IBAction)chooosePic:(id)sender { //Button to get image from albums
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { //Choose image
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { //if cancelled before selection
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Works Fine Try
I have trouble figuring out, how to capture photo and record video on same view because methods used for both are same.
I tried it but still not clear whether code is correct or not.
I shall be thankful if someone would just review the code and tell me it's correct or not.
Below is the code :
- (IBAction)CaptureRubbish:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
- (IBAction)RecordRubbish:(id)sender {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
[self presentViewController:picker animated:YES completion:NULL];
}
}
- (void)videoPlayBackDidFinish:(NSNotification *)notification {
[[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
// Stop the video player and remove it from view
[self.videoController stop];
[self.videoController.view removeFromSuperview];
self.videoController = nil;
// Display a message
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Video Playback" message:#"Just finished the video playback. The video is now removed." preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okayAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:okayAction];
[self presentViewController:alertController animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self->ImageVIew.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
self.videoURL = info[UIImagePickerControllerMediaURL];
[picker dismissViewControllerAnimated:YES completion:NULL];
self.videoController = [[MPMoviePlayerController alloc] init];
[self.videoController setContentURL:self.videoURL];
[self.videoController.view setFrame:CGRectMake (0, 0, self.view.frame.size.width, 460)];
[self.view addSubview:self.videoController.view];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(videoPlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.videoController];
[self.videoController play];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
if (info[UIImagePickerControllerMediaType] == (NSString *) kUTTypeImage) {
// here your image capture code
}
else if(info[UIImagePickerControllerMediaType]== (NSString *)kUTTypeMovie)
{
// here your video capture code
}
[picker dismissViewControllerAnimated:YES completion:NULL];
}
I have created two options in the screen.
To capture the image from camera using : UIImagePickerControllerSourceTypeCamera
To load the image from the UIImagePickerControllerSourceTypeSavedPhotosAlbum.
But when i am capturing the image, image is not storing in the device?
My code is:
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
NSLog(#"From didDismissWithButtonIndex - Selected Option: %#", [actionSheet buttonTitleAtIndex:buttonIndex]);
NSString*image=[actionSheet buttonTitleAtIndex:buttonIndex];
if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:#"Camera"]) {
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:nil message:#"Device Camera Is Not Working" delegate:nil cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alert show];
return;
}
else{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
}
else if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:#"Gallery"])
{
UIImagePickerController *pickerView = [[UIImagePickerController alloc] init];
pickerView.allowsEditing = YES;
pickerView.delegate = self;
[pickerView setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
//[pickerView setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:pickerView animated:YES completion:nil];
}
}
#pragma mark - PickerDelegates
//=====================================Image picker ===============================================
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage* orginalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:isRowIndex inSection:isSectionIndex] ;
UITableViewCell *cell = [jobstable cellForRowAtIndexPath:indexPath];
UIImageView *tableIMAGE=(UIImageView *)[cell.contentView viewWithTag:19];
tableIMAGE.image=orginalImage;
imageStris = [UIImageJPEGRepresentation(tableIMAGE.image,1)base64Encoding];
answersARRAY[indexPath.row] = [NSString stringWithFormat:#"-1,%#,%#",answersARRAY[indexPath.row],imageStris];
[self visubull];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Just put this line in - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ to store image
When you capture image you have to store it manually.
UIImageWriteToSavedPhotosAlbum(orginalImage,nil, nil, nil);
Hope this will help you
It's because you're not doing anything with the image. You assign it as b64 string to an array and that's it. You must save it to the camera roll manually:
UIImageWriteToSavedPhotosAlbum(tableIMAGE.image, nil, nil, nil);
You have to save image camera roll in didFinishPickingMediaWithInfo like this
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
if(picker.sourceType==UIImagePickerControllerSourceTypeCamera)
{
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
[PHAssetChangeRequest creationRequestForAssetFromImage:image];
} completionHandler:nil];
}
}
I Would create a app with camera but when i write this code in ImageViewController.m
xcode: EXPECTED METODY BODY and "."
how i can fix this?
thanks
-(BOOL) launchCameraControllerFromViewController: (UIViewController*) controller usingDelegate: (id <UIImagePickerControllerDelegate, UINavigationControllerDelegate>)
BOOL truefalse = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]; //variable to check whether there is a camera available
//if there is a camera, the delegate passed exists, and the controller passed exists, proceed on, otherwise don't go any further
if (!truefalse || (delegate == nil) || (controller == nil)) {
NSLog(#"no can do, delegate/camera/view controller doesn't exist!");
return NO;
}
UIImagePickerController *cameraController = [[UIImagePickerController alloc] init];
cameraController.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraController.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
cameraController.allowsEditing = NO;
cameraController.delegate = delegate;
It says you have not method at all. Try changing to this (note the braces after method name and after all code):
-(BOOL) launchCameraControllerFromViewController: (UIViewController*) controller usingDelegate: (id <UIImagePickerControllerDelegate, UINavigationControllerDelegate>) delegate{
BOOL truefalse = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]; //variable to check whether there is a camera available
//if there is a camera, the delegate passed exists, and the controller passed exists, proceed on, otherwise don't go any further
if (!truefalse || (delegate == nil) || (controller == nil)) {
NSLog(#"no can do, delegate/camera/view controller doesn't exist!");
return NO;
}
UIImagePickerController *cameraController = [[UIImagePickerController alloc] init];
cameraController.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraController.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
cameraController.allowsEditing = NO;
cameraController.delegate = delegate;
}
Can You Try this.
- (IBAction)presentPicker:(id)sender {
// **********************************************
// * Show action sheet that will allow image selection from camera or gallery
// **********************************************
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:(id)self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Image from Camera", #"Image from Gallery", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
actionSheet.alpha=0.90;
actionSheet.tag = 1;
[actionSheet showInView:[UIApplication sharedApplication].keyWindow];
}
#pragma mark - Image picker methods
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex: (NSInteger)buttonIndex {
switch (actionSheet.tag) {
case 1:
switch (buttonIndex) {
case 0:
[self showCameraImagePicker];
break;
case 1:
[self showGalleryImagePicker];
break;
}
break;
default:
break;
}
}
- (void)showCameraImagePicker {
#if TARGET_IPHONE_SIMULATOR
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Simulator" message:#"Camera not available." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
#elif TARGET_OS_IPHONE
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
// [(UIViewController *)self.delegate presentModalViewController:picker animated:YES];
#endif
}
- (void)showGalleryImagePicker {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
// [(UIViewController *)self.delegate presentModalViewController:picker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage: (UIImage *)image editingInfo:(NSDictionary *)editingInfo {
// [picker dismissModalViewControllerAnimated:NO];
[picker dismissViewControllerAnimated:YES completion:NULL];
photo.image = image;
//[self presentImageCropperWithImage:image];
}
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {
// [picker dismissModalViewControllerAnimated:NO];
[picker dismissViewControllerAnimated:NO completion:nil];
// Extract image from the picker
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:#"public.image"]){
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
photo.image = image;
[picker dismissViewControllerAnimated:YES completion:NULL];
// [self presentImageCropperWithImage:image];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
I am trying to push a view controller (for extra editing) from UIImagePickerController after user has selected an image.
It is successfully pushed.
But when that view controller is popped and user return to the editing screen, the editing screen is no longer interactable. Does anyone know what is the problem.
-(void)didTapBtn:(id)sender
{
self.picker = [[UIImagePickerController alloc] init];
self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.picker.delegate = self;
self.picker.allowsEditing = YES;
[self presentViewController:self.picker animated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
if (image == nil) {
image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
if (image == nil) {
return;
}
self.test = [[BTTestViewController alloc] init];
self.test.delegate = self;
[self.picker pushViewController:self.test animated:YES];
}
//Called by BTTestViewController
-(void)didCancel
{
[self.picker popViewControllerAnimated:YES];
}
try this one it work in my application:
UIImagePickerController *imagePicker;//put it in .h file
put it in .m file
imagePicker = [[UIImagePickerController alloc]init];
imagePicker.delegate = self;
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.allowsEditing = YES;
[self presentViewController:imagePicker animated:YES completion:nil];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"STEP-BY-STEP-STORY!" message: #"Camera is not available" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *pickedImage = [info objectForKey:UIImagePickerControllerEditedImage];
imageData.image =[self resizeImage:imageData.image width:200 height:200];
[picker dismissViewControllerAnimated:YES completion:nil];
iphone_filtersViewController *nav=[[iphone_filtersViewController alloc]initWithNibName:#"iphone_filtersViewController" bundle:nil];
[self.navigationController pushViewController:nav animated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
// release Picker
[picker dismissViewControllerAnimated:YES completion:nil];
}
// iphone_filtersViewController method
-(IBAction)backButton
{
[self.navigationController popViewControllerAnimated:YES];
}
may be it will help.