Search code examples
iosobjective-cuiimagepickercontroller

UIImagePickerControllerEditedImage get nil


Hey guys I'm doing some image editing with UIImagePickerController. Here is some code in imagePickerController:didFinishPickingMediaWithInfo:

    UIImage *editedImg = [info objectForKey:UIImagePickerControllerEditedImage];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:editedImg];
    CGRect imgFrm = imgView.frame;
    float rate = imgFrm.size.height / imgFrm.size.width;
    imgFrm.size.width = size;
    imgFrm.size.height = size * rate;
    imgFrm.origin.x = 0;
    imgFrm.origin.y = (size - imgFrm.size.height) / 2;
    [imgView setFrame:imgFrm];

    UIView *cropView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size, size)];
    [cropView setBackgroundColor:[UIColor blackColor]];
    [cropView addSubview:imgView];

    UIImage *croppedImg = [MyUtil createUIImageFromUIView:cropView];

The above is to set the image in a size*size view and draw a image from a view when the height of the image returned by picker is smaller than size.

Here is the code of createUIImageFromUIView:(UIView*)view :

+ (UIImage *)createUIImageFromUIView:(UIView *)view
{

    UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 2.0);

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:ctx];

    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return viewImage;
}

My problem is : when debugging, the 'editedImg'(defined in first line) just shows 'nil'. But, the following code works well. I get the corpView(shows 'nil' too) correctly and get cropped image and can encode it to base64 encoded string for sending to server side. I just want to know why the editedImg is nil(returned by [info objectForKey:UIImagePickerControllerEditedImage], but when I choose to print the info in debug mode, the output is not nil in the console)?


Solution

  • After some searching I accidentally found this : string value always shows nil in objective-c

    This is the reason why I always see 'nil' in debug mode while the code works well.