iphoneiosxcodeios4

How do you crop an image in iOS


I have a photo app where you can add stickers in one section. When you're finished I want to save the image. Here is the code that I have to do that.

if(UIGraphicsBeginImageContextWithOptions != NULL)
{
    UIGraphicsBeginImageContextWithOptions(self.view.frame.size, YES, 2.5);
} else {
    UIGraphicsBeginImageContext(self.view.frame.size);
}
CGContextRef contextNew=UIGraphicsGetCurrentContext();

[self.view.layer renderInContext:contextNew];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

Now the image that gets saved is the full screen of the image, which is fine, but now I need to crop the image and I don't know how. You can see the image at the link below: http://dl.dropbox.com/u/19130454/Photo%202012-04-09%201%2036%2018%20PM.png

I need to crop: 91px from the left and right 220px from the bottom

Any help would be greatly appreciated. If I haven't explained things clearly, please let me know and I'll do my best to re-explain.


Solution

  • How about something like this

    CGRect clippedRect  = CGRectMake(self.view.frame.origin.x+91, self.view.frame.origin.y, self.view.frame.size.width-91*2, self.view.frame.size.height-220);
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], clippedRect);
    UIImage *newImage   = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);