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);
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).
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!