flite3 flite3 - 1 year ago 134
iOS Question

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 {
CGContextRef contextNew=UIGraphicsGetCurrentContext();

[self.view.layer renderInContext:contextNew];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();


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:

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.

Answer Source

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];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download