How to Crop an Image (UIImage) On iOS

Thu, Jun 3

In October of 2012 I wrote another example to crop an image, which requires a lot less code.

This post shows an example of one way to crop an image. Let’s begin by looking at a screenshot of the original and cropped image on the iPhone simulator:

As you can see, I am cropping a rectangle from the middle of the image on the top left. The code below shows how to accomplished this:

// Create the image from a png file
UIImage *image = [UIImage imageNamed:@"prgBinary.jpg"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
 
// Get size of current image
CGSize size = [image size];
 
// Frame location in view to show original image
[imageView setFrame:CGRectMake(0, 0, size.width, size.height)];
[[self view] addSubview:imageView];
[imageView release];	
 
// Create rectangle that represents a cropped image  
// from the middle of the existing image
CGRect rect = CGRectMake(size.width / 4, size.height / 4 , 
    (size.width / 2), (size.height / 2));
 
// Create bitmap image from original image data,
// using rectangle to specify desired crop area
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
UIImage *img = [UIImage imageWithCGImage:imageRef]; 
CGImageRelease(imageRef);
 
// Create and show the new image from bitmap data
imageView = [[UIImageView alloc] initWithImage:img];
[imageView setFrame:CGRectMake(0, 200, (size.width / 2), (size.height / 2))];
[[self view] addSubview:imageView];
[imageView release];

All the heavy lifting is done by the method CGImageCreateWithImageInRect, which creates a new bitmap image using existing image data and a rectangle that is a subregion of that same image. Once cropped, I create a new UIImageView and add it as a subview of the current view.

21 comments

Awesome, this totally rocks! Thanks

by Poko on Jun 6, 2010. Reply #

Nice job.
I used the “CGImageCreateWithImageInRect” to implement a Util to crop image.

by handy.wang on Jun 8, 2010. Reply #

Thank you, it helps a lot.

by anders on Jul 20, 2010. Reply #

Great job. I was searching correct way of crop image. No memory leak.
Cheers

by Chinthaka Kumarasiri on Jul 22, 2010. Reply #

Thanks, this saved me time rummaging through all the CG library options!

by Pip on Aug 16, 2010. Reply #

Thanks

by munching on Jan 24, 2011. Reply #

Great work dude em loving it :)

by sanaullah on Feb 2, 2011. Reply #

Thank you. It’s very usefull.

by cetauri on Feb 8, 2011. Reply #

Thanks Really… Its a very useful link

by Abhishek Dangwal on Jul 24, 2011. Reply #

Thanks, works, well!

by James on Jul 30, 2011. Reply #

Thanks! That’s a great solution, provided you aren’t using display specific images (normal vs. retina). In that case, I would add something like CGFloat scale = ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2.0) ? 2.0 : 1.0; and change the assignment of img to UIImage *img = [UIImage imageWithCGImage:imageRef scale:scale orientation:]; You will also need to multiply your size dimensions by scale as well.

by Jon on Aug 26, 2011. Reply #

Thanks,…….. it’s really good job.

by Prakash K iphone on Oct 8, 2011. Reply #

Very Helpful for new user …………

by Dinesh Pawar on Nov 21, 2011. Reply #

incredeble app.. we need more options while cropping the pic..!!!

by mahesh on Jan 1, 2012. Reply #

Thank You…

by Nilesh Tripathi on Feb 28, 2012. Reply #

That’s great :)
I extended from it to extract from various positions (Like top-left, center, top-right, etc):

Hope it helps.

+ (UIImage*) cropImageFromLargeImage:(UIImage*)image withSize:(CGRect)rect withPosition:(NSString *)position
{
// Get size of current image
CGSize size = [image size];

int myWidth= 172;
int myHeight = 104;

CGRect newRect;

if (position == @”topleft”){
newRect= CGRectMake(0, 0, myWidth, myHeight);
} else if (position == @”center”){
newRect= CGRectMake(((size.width/2) – (myWidth/2)), ((size.height/2)-(myHeight/2)), myWidth, myHeight);
} else if (position == @”topright”){
newRect= CGRectMake(size.width-myWidth, 0, myWidth, myHeight);
} else if (position == @”bottomleft”){
newRect= CGRectMake(0, (size.height-myWidth), myWidth, myHeight);
} else if (position == @”bottomright”){
newRect= CGRectMake(size.width-myWidth, size.height-myHeight, myWidth, myHeight);
}

// Create bitmap image from original image data,
// using rectangle to specify desired crop area
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], newRect);
UIImage *img = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

return img;
}

by Rahul Vagadiya on May 27, 2012. Reply #

thank you, it worked

by taymour on Jul 30, 2012. Reply #

thanks…its rocks with one shot…grt..

by bincy on Nov 8, 2012. Reply #

Thank you very much.

by Shanmu on Feb 7, 2013. Reply #

Thanks.

by Vineesh on Nov 11, 2013. Reply #

What about if the original image is rotated or scaled and translated ?

by Giovanni Vaccarino on Dec 17, 2013. Reply #

Leave a Comment