2

So I have a UIImage which I want to crop. I looked and found imageByCroppingToRect method for CIImage. So, I converted the data to CIImage instead of UIImage, crop it using the specified method and then convert the resulting CIImage to UIImage and then display it in a UIImageView.

My code is

 NSData *data = [[NSData alloc]initWithData:[def objectForKey:@"imageData"]];
//UIImage *normalImage = [[UIImage alloc]initWithData:data];
CIImage *originalImage = [CIImage imageWithData:data];
[originalImage imageByCroppingToRect:CGRectMake(10, 72, 300, 300)];

self.imageView.image = [UIImage imageWithCIImage:originalImage];

The problem is the image gets rotated by 90 degrees and I am not sure if it is being cropped. This image is captured using the device's camera. I use AVFoundation to access the camera. My session preset is AVCaptureSessionPresetPhoto. I think this is why I get the zooming.

nupac
  • 2,459
  • 7
  • 31
  • 56
  • The image is probably rotated because the image is in another orientation and flipped using an EXIF tag. Some image formats like JPEG use lossy compression. Rotating and re-saving an image just to correct the orientation would compress the image further. Therefore the image itself is kept, but metadata is introduced to mean "this image is supposed to be rotated by 90, 180 or 270 degrees". – Jesper Oct 14 '13 at 09:45

2 Answers2

3
CGRect rect = CGRectMake(10, 72, 300, 300);
CGImageRef imref = CGImageCreateWithImageInRect([yourOriginalImage CGImage], rect);
UIImage *newSubImage = [UIImage imageWithCGImage:imref];

try this. may help u.

EDIT:

Firstly fix your image orientation: refs : https://github.com/j3r3miah/mapmatic-ios/blob/master/Mapmatic/UIImage+FixOrientation.m

then use above code to crop the Image to Specified Rect.

Nookaraju
  • 1,668
  • 13
  • 24
  • does not work, I still have the rotation problem. Also it zoooms a lot using this technique. – nupac Oct 14 '13 at 09:35
  • if yourImage is retina image then u need to give rect like CGRectMake(20, 144, 600, 600) – Nookaraju Oct 14 '13 at 09:40
  • I still get the zoom. I am getting this image from the camera. I use AVFoundation to access the camera. My session preset is `AVCaptureSessionPresetPhoto`. Do you think thats why this is happening? – nupac Oct 14 '13 at 09:52
  • 2
    might be the issue. firstly fix image orientation and try. https://github.com/j3r3miah/mapmatic-ios/blob/master/Mapmatic/UIImage+FixOrientation.m – Nookaraju Oct 14 '13 at 09:55
  • yes the orientation is fixed. Thank you. I think the image is huge thats why the zoom thing. I'll try to come up with a new more appropriate rect size – nupac Oct 14 '13 at 10:07
  • So the correct CGRect values are 62, 407, 1812, 1688, Works great. – nupac Oct 14 '13 at 10:36
  • if you, either add the link to the answer with a little description of the problem or just direct the users to the comment section, I would accept your answer. Thanks for your help – nupac Oct 14 '13 at 10:38
1

Not really an answer to your question, but an answer to your problem

https://github.com/mbcharbonneau/UIImage-Categories

especially this file : https://github.com/mbcharbonneau/UIImage-Categories/blob/master/UIImage%2BResize.m

- (UIImage *)croppedImage:(CGRect)bounds {
    CGFloat scale = MAX(self.scale, 1.0f);
    CGRect scaledBounds = CGRectMake(bounds.origin.x * scale, bounds.origin.y * scale, bounds.size.width * scale, bounds.size.height * scale);
    CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], scaledBounds);
    UIImage *croppedImage = [UIImage imageWithCGImage:imageRef scale:self.scale orientation:UIImageOrientationUp];
    CGImageRelease(imageRef);
    return croppedImage;
}

you will find there all you need to crop your image

Jerome Diaz
  • 1,746
  • 8
  • 15