/ Published in: Objective C
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
{ CVPixelBufferRef buffer = NULL; // config size_t width = [image size].width; size_t height = [image size].height; size_t bitsPerComponent = 8; // *not* CGImageGetBitsPerComponent(image); CGColorSpaceRef cs = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); CGBitmapInfo bi = kCGImageAlphaNoneSkipFirst; // *not* CGImageGetBitmapInfo(image); NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey, [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey, nil]; // create pixel buffer CVPixelBufferCreate(kCFAllocatorDefault, width, height, k32ARGBPixelFormat, (CFDictionaryRef)d, &buffer); CVPixelBufferLockBaseAddress(buffer, 0); void *rasterData = CVPixelBufferGetBaseAddress(buffer); size_t bytesPerRow = CVPixelBufferGetBytesPerRow(buffer); // context to draw in, set to pixel buffer's address CGContextRef ctxt = CGBitmapContextCreate(rasterData, width, height, bitsPerComponent, bytesPerRow, cs, bi); if(ctxt == NULL){ NSLog(@"could not create context"); return NULL; } // draw [image compositeToPoint:NSMakePoint(0.0, 0.0) operation:NSCompositeCopy]; CVPixelBufferUnlockBaseAddress(buffer, 0); CFRelease(ctxt); return buffer; }
URL: http://lists.apple.com/archives/quartz-dev/2006/Oct/msg00096.html