Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 483
flite3 Avatar asked Apr 09 '12 20:04

flite3


2 Answers

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); 
like image 167
lukasz Avatar answered Oct 05 '22 23:10

lukasz


Following code may help you.

You should get the correct cropFrame fist by below method

-(CGRect)cropRectForFrame:(CGRect)frame {     // NSAssert(self.contentMode == UIViewContentModeScaleAspectFit, @"content mode must be aspect fit");      CGFloat widthScale = imageview.superview.bounds.size.width / imageview.image.size.width;     CGFloat heightScale = imageview.superview.bounds.size.height / imageview.image.size.height;      float x, y, w, h, offset;     if (widthScale<heightScale) {         offset = (imageview.superview.bounds.size.height - (imageview.image.size.height*widthScale))/2;         x = frame.origin.x / widthScale;         y = (frame.origin.y-offset) / widthScale;         w = frame.size.width / widthScale;         h = frame.size.height / widthScale;     } else {         offset = (imageview.superview.bounds.size.width - (imageview.image.size.width*heightScale))/2;         x = (frame.origin.x-offset) / heightScale;         y = frame.origin.y / heightScale;         w = frame.size.width / heightScale;         h = frame.size.height / heightScale;     }     return CGRectMake(x, y, w, h); } 

Then you need to call this method to get cropped image

- (UIImage *)imageByCropping:(UIImage *)image toRect:(CGRect)rect {     // you need to update scaling factor value if deice is not retina display     UIGraphicsBeginImageContextWithOptions(rect.size,                                            /* your view opaque */ NO,                                            /* scaling factor */ 2.0);      // 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; } 
like image 36
dev_binod Avatar answered Oct 06 '22 00:10

dev_binod