Sabbath
Sabbath

Reputation: 91

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)?

Upvotes: 0

Views: 1484

Answers (3)

Maulik
Maulik

Reputation: 19418

You can get your cropped image size by

UIImage *croppedImg = [MyUtil createUIImageFromUIView:cropView];
NSData *dataForImage = UIImagePNGRepresentation(croppedImg);

Now you can check length

if (dataForImage.length)
{

}

Upvotes: 0

Sabbath
Sabbath

Reputation: 91

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.

Upvotes: 0

wormlxd
wormlxd

Reputation: 514

The editdImg gets nil, try:

UIImage *editedImg = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

Get file size:

- (long long) fileSizeAtPath:(NSString*) filePath{
  NSFileManager* manager = [NSFileManager defaultManager];
  if ([manager fileExistsAtPath:filePath]){
      return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
  }
  return 0;
}

Best wishes!

Upvotes: 1

Related Questions