Created
December 16, 2010 09:10
-
-
Save veritech/743217 to your computer and use it in GitHub Desktop.
Processing Images on a background thread, and wrapping them up in NSOperations ... just to be pretty
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
#import <Foundation/Foundation.h> | |
#import <QuartzCore/QuartzCore.h> | |
@interface FRImageOperation : NSOperation { | |
} | |
@end | |
@implementation FRImageOperation | |
- (id)init{ | |
if( (self = [super init]) ){ | |
//Do some custom initialization | |
} | |
return self; | |
} | |
- (void)dealloc { | |
[super dealloc]; | |
} | |
- (void) main{ | |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; | |
////////////////////////////////////////// | |
UIImage *backgroundImg; | |
CGLayerRef layer; | |
CGImageRef resultImage; | |
CGContextRef context, layerContext; | |
void *bitmapData; | |
CGColorSpaceRef colorSpace; | |
CGSize canvasSize; | |
int bitmapByteCount; | |
int bitmapBytesPerRow; | |
//Get the background image | |
backgroundImg = [UIImage imageNamed:@"sample.jpg"]; | |
//Initialize the canvas size! | |
canvasSize = [backgroundImg size]; | |
// | |
bitmapBytesPerRow = (canvasSize.width * 4); | |
bitmapByteCount = (bitmapBytesPerRow * canvasSize.height); | |
//Create the color space | |
colorSpace = CGColorSpaceCreateDeviceRGB(); | |
bitmapData = malloc( bitmapByteCount ); | |
//Check the the buffer is alloc'd | |
if( bitmapData == NULL ){ | |
DebugLog(@"Buffer could not be alloc'd"); | |
} | |
//Create the context | |
context = CGBitmapContextCreate(bitmapData, canvasSize.width, canvasSize.height, 8, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast); | |
if( context == NULL ){ | |
DebugLog(@"Context could not be created"); | |
} | |
layer = CGLayerCreateWithContext(context, canvasSize, NULL); | |
if( layer == NULL ){ | |
DebugLog(@"Layer could not be created"); | |
} | |
layerContext = CGLayerGetContext(layer); | |
if( layerContext == NULL){ | |
DebugLog(@"No Layer context"); | |
} | |
//Draw the image into the bitmap context | |
CGContextDrawImage(context, CGRectMake(0.0f,0.0f,backgroundImg.size.width,backgroundImg.size.height), [backgroundImg CGImage]); | |
//Fill color set to green | |
CGContextSetRGBFillColor(layerContext,0.33f,0.66f,0.33f,1.0f ); | |
//Draw a green square | |
CGContextFillRect(layerContext, CGRectMake(0.0f,0.0f,200.0f,200.0f)); | |
//Draw the layer in the context | |
CGContextDrawLayerAtPoint( context, CGPointMake(0.0f, 0.0f), layer); | |
//Get the result image | |
resultImage = CGBitmapContextCreateImage(context); | |
//Save the image to the photos album | |
UIImageWriteToSavedPhotosAlbum( [UIImage imageWithCGImage:resultImage],self,@selector(image:error:context:),NULL); | |
//Cleanup | |
free(bitmapData); | |
CGColorSpaceRelease(colorSpace); | |
CGImageRelease(resultImage); | |
DebugLog(@"Complete Processing"); | |
////////////////////////////////////////// | |
[pool drain]; | |
} | |
-(void) image:(UIImage*) aImage error:(NSError*) aError context:(void *) aContext{ | |
DebugLog(@"Completed saving"); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment