CGImageCreateWithImageInRect不正确地裁剪 [英] CGImageCreateWithImageInRect not Cropping Correctly
问题描述
我有一段时间使用CGImageCreateWithImageInRect裁剪照片。我的应用程序有用户移动/放大图像,直到它填充316×316视图,然后我想要裁剪框外任何区域,并将图像保存为UIImage。我通过获取x和y图像原始的绝对值,确定裁剪的原点,让我回到0,0的视图/框包含imageview。下面是代码:
I'm having a hell of a time using CGImageCreateWithImageInRect to crop a photo. My app has the user move / enlarge an image till it fills a 316 x 316 view, then I want it to crop off any area outside the box and save the image as a UIImage. I determine the crop origin by taking the absolute value of the x and y imageview origin as that brings me back to 0,0 of the view / box that contains the imageview. Below is the code:
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的起源,这是相当一些从应该采取的领域(有时我得到奇怪的结果)。
This is driving me crazy, I just can't get it to crop the area I want. I've got it to scale correctly, but the problem seems to be with the x and y origin of the crop rect, it's quite a bit off from the area it should be taking (and sometimes I get weird results).
此外,对于使用手机相机拍摄的图像,我必须将我裁剪矩形中的所有内容乘以2,因为它是正确的大小。很奇怪。
Also, it seems for images taken with the phones camera, I have to multiply everything in my crop rect by 2 for it to be the correct size. Very weird.
所以我想知道,有没有人知道如何解决这个问题,或任何人都有一个更好的方式裁剪(请记住我需要裁剪我的照片中的一个区域)。谢谢!
So I'm wondering, does anyone know how to fix this, or does anyone possible have a better way of cropping (and please keep in mind that I need to crop an area within my photo). Thanks!
推荐答案
我发现了问题。我在缩放图像之前尝试抓取裁剪区域。现在必须弄清楚如何减少图像之前,我做CGImageCreateWithImageInRect。
I found the problem. I was trying to grab the crop area before I scaled the image. Now have to figure out how to reduce the image before I do "CGImageCreateWithImageInRect".
这篇关于CGImageCreateWithImageInRect不正确地裁剪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!