2011-11-09 61 views
4

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

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原点有关,它离它应该采取的区域有点偏离(有时我会得到奇怪的结果)。

此外,它似乎与手机相机拍摄的图像,我必须乘以我的作物rect一切为2,因为它是正确的大小。很奇怪。

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

+0

请在答案中提供您的解决方案,并在系统允许时接受。 –

回答

0

我发现了这个问题。我在缩放图像之前试图抓取裁剪区域。现在必须弄清楚如何在“CGImageCreateWithImageInRect”之前减少图像。

4

有关于这个话题这么多的帖子与他们具有相同的确切答案, 利用:CGImageCreateWithImageInRect 但它们都没有完全解决什么是试图要裁剪小姐的定位的问题。

但是,只有一个线程(深埋在内心深处),有进去 CGImageCreateWithImageInRect(image.CGImage,croppedRect一个小细节,每个人都缺少... How to crop a UIImageView to a new UIImage in 'aspect fit' mode?

的“矩形”参数) 需要表示(取自)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; 
} 
2

我想这基于我自己的经验提供更多信息/答案。

UIImage(从而CGImageCreateWithImageInRect:)角度来看,(0, 0)位于左下方。在确定作物广场,你可以简单地计算基于标准纵向方形,然后使用@温室的回答方法(贴在这里再次清楚和改用CGFloat来修复铸件的错误。

- (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; 
} 

此代码将裁切方块移动到正确的相对位置

+0

完美适合我 –