0

I'm trying to take image using UIImagePickerController. If the property allowEditing is NO then everything working fine. But as soon as I change the property value to YES no image is coming. It's occuring in both Camera and Library.

Below is the code that executed when button is tapped for opening camera.

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIImagePickerController *controller_imagePicker = [[UIImagePickerController alloc] init];
        [controller_imagePicker setDelegate:self];
        [controller_imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
        [controller_imagePicker setAllowsEditing:YES];
        controller_imagePicker.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        [self presentViewController:controller_imagePicker animated:YES completion:nil];
    }

And when we take the image and edits it and use the image. black image is coming in the respecitve imageview.

Also as soon as the camera opens I receive this warning: "Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates."

Any Idea??

Rohitax Rajguru
  • 893
  • 2
  • 13
  • 35

2 Answers2

0

You should use UIImagePickerControllerEditedImage key instead of UIImagePickerControllerOriginalImage if allowsEditing.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *image = nil;
    if ([picker allowsEditing]){
        image = [info objectForKey:UIImagePickerControllerEditedImage];
    } else {
        image = [info objectForKey:UIImagePickerControllerOriginalImage];
    }
    // do something
}
zy.liu
  • 346
  • 4
  • 15
  • No success dear. Any other idea?? – Rohitax Rajguru Aug 17 '15 at 14:08
  • The warning you got seems like a same question with this :http://stackoverflow.com/questions/18890003/uiimagepickercontroller-error-snapshotting-a-view-that-has-not-been-rendered-re – zy.liu Aug 17 '15 at 14:20
  • Can you put a breakpoint on `imagePickerController:didFinishPickingMediaWithInfo:` method and print object of the `image` you got in lldb? Is it nil? And the `(NSDictionary *)info` object? – zy.liu Aug 17 '15 at 14:22
  • Thanx for the efforts @zy.liu. It got to be wrong due to a category which was rendering the image. I removed that one and now its working totally fine. – Rohitax Rajguru Aug 17 '15 at 15:06
0
#import <MobileCoreServices/UTCoreTypes.h>
….
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
    UIImagePickerController *imagePickerCamera =[[UIImagePickerController alloc] init];
    imagePickerCamera.delegate = self;
    imagePickerCamera.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage,nil];
    imagePickerCamera.allowsEditing = YES;
    imagePickerCamera.sourceType = UIImagePickerControllerSourceTypeCamera;

    [self presentViewController:imagePickerCamera  animated:YES completion:nil];
}

else if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
    UIImagePickerController *imagePickerAlbum =[[UIImagePickerController alloc] init];
    imagePickerAlbum.delegate = self;
    imagePickerAlbum.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage,nil];
    imagePickerAlbum.allowsEditing = YES;
    imagePickerAlbum.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentViewController:imagePickerAlbum animated:YES completion:nil];
}


#pragma mark - ImagePickerController Delegate

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

    if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone) {
        [picker dismissViewControllerAnimated:YES completion:nil];
    }
    else
    {
        [popover dismissPopoverAnimated:YES];
    }


    UIImage* originalImage = nil;
    originalImage = [info objectForKey:UIImagePickerControllerEditedImage];

    if(originalImage==nil)
    {
        originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    }

    if(originalImage==nil)
    {
        originalImage = [info objectForKey:UIImagePickerControllerCropRect];
    }
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
Mehul
  • 3,033
  • 23
  • 37