简体   繁体   中英

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

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!

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

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM