5
- (UIImage *)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
{
    CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
    UIImage *cropped = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    return cropped;
}

I am Using this code. Please give some solutions.Thanks in advance

Mani
  • 1,310
  • 1
  • 20
  • 40
  • sample codes and tutorials.... http://developer.apple.com/library/mac/#/legacy/mac/library/samplecode/Cropped_Image/Introduction/Intro.html http://www.icodeblog.com/2010/10/14/working-with-uigesturerecognizers/ http://stackoverflow.com/questions/7950719/how-to-crop-the-image-in-iphone http://www.samwirch.com/blog/cropping-and-resizing-images-camera-ios-and-objective-c http://mobiledevelopertips.com/graphics/how-to-crop-an-image.html – Ayaz Sep 05 '12 at 12:43

1 Answers1

4

CGImageCreateWithImageInRect does not handle image orientation correctly. There are many strange and wonderful cropping techniques out there on the net involving giant switch/case statements (see the links in Ayaz's answer), but if you stay at the UIKit-level and use only methods on UIImage itself to do the drawing, all the nitty gritty details are taken care for you.

The following method is as simple as you can get and works in all cases that I have encountered:

- (UIImage *)imageByCropping:(UIImage *)image toRect:(CGRect)rect
{
    if (UIGraphicsBeginImageContextWithOptions) {
        UIGraphicsBeginImageContextWithOptions(rect.size,
                                               /* opaque */ NO,
                                               /* scaling factor */ 0.0);
    } else {
        UIGraphicsBeginImageContext(rect.size);
    }

    // stick to methods on UIImage so that orientation etc. are automatically
    // dealt with for us
    [image drawAtPoint:CGPointMake(-rect.origin.x, -rect.origin.y)];

    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return result;
}

You may want to change the value of the opaque argument if you don't need transparency.

Mike Weller
  • 45,401
  • 15
  • 131
  • 151
  • Thank for your reply. This also working for my old source code. My problem is crop image is much more zoom and blurred, this problem occurred only on the device. Any other idea kindly help me. – Mani Sep 05 '12 at 13:21
  • It sounds like a problem with retina scales. The code I posted will use the screen's scale in the resulting image, but you need to be careful that the crop rectangle you pass in is in points, not pixels. – Mike Weller Sep 05 '12 at 13:24
  • How to pass pixels. can you explain please, not understanding your answer. How to handle the retina display crop issue. – Mani Sep 05 '12 at 13:43
  • It's hard to help further without seeing more of your code. If the method I have provided does not work, it means you are not providing a correct crop rectangle. – Mike Weller Sep 05 '12 at 13:51
  • I am called the method like this [self imageByCropping:cropImage toRect:CGRectMake(85, 83, 150, 150)]; Any thing mistake for my side. – Mani Sep 05 '12 at 14:01
  • @Mike-Weller Hey Mike, I am having a similar issue, where I am using a scroll view to zoom and crop the image. If I use your technique, I have a problem getting the crop rectangle to match what I set on-screen. My code: `CGFloat scale = self.cropScrollView.zoomScale; CGFloat imageHeight = self.cropImageView.image.size.height * scale; CGRect cropRect = CGRectMake(self.cropScrollView.contentOffset.x, roundf(imageHeight - self.cropScrollView.bounds.size.height - self.cropScrollView.contentOffset.y), self.cropScrollView.bounds.size.width*2, self.cropScrollView.bounds.size.height*2);` – Raconteur Feb 11 '13 at 16:49