CGImageCreateWithImageInRect没有正确裁剪

时间:2011-11-09 06:57:26

标签: iphone objective-c ios cocoa-touch core-graphics

我有一段时间使用CGImageCreateWithImageInRect裁剪照片。我的应用程序让用户移动/放大图像,直到它填充316 x 316视图,然后我希望它裁剪框外的任何区域并将图像保存为UIImage。我通过获取x和y imageview原点的绝对值来确定裁剪原点,因为它将我带回到包含imageview的视图/框的0,0。以下是代码:

    CGRect croppedRect = CGRectMake(fabs(widthDistanceToOrigin), fabs(HeightDistanceToOrigin), 316, 316);
    CGImageRef croppedImageRef = CGImageCreateWithImageInRect(image.CGImage, croppedRect); 
    UIImage *croppedImage = [UIImage imageWithCGImage: croppedImageRef scale: 1 / (imageScale) orientation: image.imageOrientation];
    CGImageRelease(croppedImageRef); 

这让我发疯,我只是无法让它去裁剪我想要的区域。我已经让它正确缩放,但问题似乎是作物矩形的x和y原点,它应该从它应该采取的区域(有时我得到奇怪的结果)相当少。

此外,对于使用手机摄像头拍摄的图像,我必须将裁剪矩形中的所有内容乘以2才能使其尺寸正确。非常奇怪。

所以我想知道,有没有人知道如何解决这个问题,或者有没有人有更好的裁剪方法(请记住我需要在我的照片中裁剪一个区域)。谢谢!

3 个答案:

答案 0 :(得分:4)

关于这个话题有很多帖子,所有人都有相同的答案, 利用:CGImageCreateWithImageInRect 但是没有一个能完全解决试图裁剪的错误定位问题。

然而,只有一个线程(深埋在内部),有一个其他人都缺失的细节... How to crop a UIImageView to a new UIImage in 'aspect fit' mode?

进入的'rect'参数 CGImageCreateWithImageInRect(image.CGImage,croppedRect) 需要表示(取自)UIImage,而不是UIImageView。

这应解决坐标系上错过定位的问题。

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:   (NSDictionary *)info
 {
        oImage = [info valueForKey:UIImagePickerControllerOriginalImage];
        NSLog(@"Original Image size width: %f x height: %f", oImage.size.width, oImage.size.height);


        // crop image according to what the image picker view is displaying
        CGRect rect = CGRectMake(0, 40, 960.0f, 960.0f); // note: 960 x 1280 is what natively comes from capturing and image
        nImage = [BGViewModifiers cropImage:oImage inRect:rect];

        // scale image down for display and creating kombie
        CGSize size = CGSizeMake(320.0f, 320.0f);
        nImage = [BGViewModifiers imageFromImage:nImage scaledToSize:size];

        NSLog(@"New Image size width: %f x height: %f", [nImage size].width, [nImage size].height);
 }

 //ref: http://stackoverflow.com/a/25293588/2298002
 + (UIImage *)cropImage:(UIImage*)image inRect:(CGRect)rect
 {
    double (^rad)(double) = ^(double deg) {
       return deg / 180.0 * M_PI;
    };

    CGAffineTransform rectTransform;
    switch (image.imageOrientation) {
      case UIImageOrientationLeft:
          rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(90)), 0, -image.size.height);
          break;
      case UIImageOrientationRight:
          rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(-90)), -image.size.width, 0);
          break;
      case UIImageOrientationDown:
          rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(-180)), -image.size.width, -image.size.height);
          break;
      default:
          rectTransform = CGAffineTransformIdentity;
  };
  rectTransform = CGAffineTransformScale(rectTransform, image.scale, image.scale);

  CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectApplyAffineTransform(rect, rectTransform));
  UIImage *result = [UIImage imageWithCGImage:imageRef scale:image.scale orientation:image.imageOrientation];
  CGImageRelease(imageRef);

  return result;
 }

 + (UIImage*)imageFromImage:(UIImage*)image scaledToSize:(CGSize)newSize
 {
    UIGraphicsBeginImageContext( newSize );
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
 }

答案 1 :(得分:1)

I would like to provide more information/answer for this based on my own experience.

From the UIImage (and thus the CGImageCreateWithImageInRect:) perspective, (0, 0) is in the bottom left. When determining the crop square, you can simply calculate the square based on the standard portrait orientation and then use the method in @greenhouse's answer (posted again here for clarity and changed to use CGFloat to fix a casting error.

- (UIImage *)cropImage:(UIImage*)image toRect:(CGRect)rect {
    CGFloat (^rad)(CGFloat) = ^CGFloat(CGFloat deg) {
        return deg / 180.0f * (CGFloat) M_PI;
    };

    // determine the orientation of the image and apply a transformation to the crop rectangle to shift it to the correct position
    CGAffineTransform rectTransform;
    switch (image.imageOrientation) {
        case UIImageOrientationLeft:
            rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(90)), 0, -image.size.height);
            break;
        case UIImageOrientationRight:
            rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(-90)), -image.size.width, 0);
            break;
        case UIImageOrientationDown:
            rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(-180)), -image.size.width, -image.size.height);
            break;
        default:
            rectTransform = CGAffineTransformIdentity;
    };

    // adjust the transformation scale based on the image scale
    rectTransform = CGAffineTransformScale(rectTransform, image.scale, image.scale);

    // apply the transformation to the rect to create a new, shifted rect
    CGRect transformedCropSquare = CGRectApplyAffineTransform(rect, rectTransform);
    // use the rect to crop the image
    CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, transformedCropSquare);
    // create a new UIImage and set the scale and orientation appropriately
    UIImage *result = [UIImage imageWithCGImage:imageRef scale:image.scale orientation:image.imageOrientation];
    // memory cleanup
    CGImageRelease(imageRef);

    return result;
}

This code shifts the crop square so that it is in the correct relative position.

答案 2 :(得分:0)

我发现了问题。在我缩放图像之前,我试图抓住裁剪区域。现在必须弄清楚如何在“CGImageCreateWithImageInRect”之前缩小图像。