How To Crop An Image (UIImage) – Take Two
October 11, 2012
In a previous post I wrote code to crop a UIImage. In this post I’ll show another way to crop with a lot less code.
The image I’ll crop is shown below:
UIImage *image = [UIImage imageNamed:@"mountains.png"]; // Create rectangle from middle of current image CGRect croprect = CGRectMake(image.size.width / 4, image.size.height / 4 , (image.size.width / 2), (image.size.height / 2)); // Draw new image in current graphics context CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], croprect); // Create new cropped UIImage UIImage *croppedImage = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef);
The cropped image looks as follows: