Skip to content

Instantly share code, notes, and snippets.

@codeswimmer
Last active December 10, 2015 12:58
Show Gist options
  • Select an option

  • Save codeswimmer/4437477 to your computer and use it in GitHub Desktop.

Select an option

Save codeswimmer/4437477 to your computer and use it in GitHub Desktop.
iOS: How To Crop Image
(UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect {
CGImageRef sourceImageRef = [image CGImage];
CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
CGImageRelease(newImageRef);
return newImage;
}
If you have implemented that function above. You can crop images using the code below (crop the image "whoeImage.png" to a rectangle (0,0,40,40)).
UImage *img = [UIimage imageNamed:@"wholeImage.png"];
UIImage *croppedImage = [self imageFromImage:img inRect:CGRectMake(0, 0, 40, 40)];
Now you have a cropped UIImage named "croppedImage".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment