Created
December 11, 2012 15:56
-
-
Save mazz/4259594 to your computer and use it in GitHub Desktop.
uses CGImageCreateWithImageInRect to perform a crop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this chunk of code loads a jpeg image into a cgimage | |
// creates a second crop of the original image with CGImageCreateWithImageInRect | |
// writes the new cropped image to the desktop | |
// ensure that the xy origin of the CGRectMake call is smaller than the width or height of the original image | |
NSURL *originalImage = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"lockwood" ofType:@"jpg"]]; | |
CGImageRef imageRef = NULL; | |
CGImageSourceRef loadRef = CGImageSourceCreateWithURL((CFURLRef)originalImage, NULL); | |
if (loadRef != NULL) | |
{ | |
imageRef = CGImageSourceCreateImageAtIndex(loadRef, 0, NULL); | |
CFRelease(loadRef); // Release CGImageSource reference | |
} | |
CGImageRef croppedImage = CGImageCreateWithImageInRect(imageRef, CGRectMake(200., 200., 100., 100.)); | |
CFURLRef saveUrl = (CFURLRef)[NSURL fileURLWithPath:[@"~/Desktop/lockwood-crop.jpg" stringByExpandingTildeInPath]]; | |
CGImageDestinationRef destination = CGImageDestinationCreateWithURL(saveUrl, kUTTypeJPEG, 1, NULL); | |
CGImageDestinationAddImage(destination, croppedImage, nil); | |
if (!CGImageDestinationFinalize(destination)) { | |
NSLog(@"Failed to write image to %@", saveUrl); | |
} | |
CFRelease(destination); | |
CFRelease(imageRef); | |
CFRelease(croppedImage); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment