Not withstanding the memory leak, you should be able to let the system handle the memory allocation for you and just use the newly created drawing to copy out the CGImageRef:
@implementation YourClass
- (CGImageRef)createCopiedImage:(CGImageRef)inImage
{
CGImageRef ret = NULL;
CGRect imageRect = CGRectMake ( 0, 0, CGImageGetWidth ( ret ), CGImageGetHeight ( ret ) );
size_t rowBytes = imageRect.size.width * 4;
size_t bufferLen = rowBytes * imageRect.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName ( kCGColorSpaceGenericRGB );
if ( colorSpace )
{
CGContextRef drawingContext = CGBitmapContextCreate ( NULL, imageRect.size.width, imageRect.size.height, 8, rowBytes, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedFirst );
if ( drawingContext )
{
CGContextDrawImage ( drawingContext, imageRect, inImage );
CFRelease ( backGroundImage );
// DO ANNOTATION STUFF HERE
// Copy image out
ret = CGBitmapContextCreateImage(drawingContext);
CGContextRelease ( drawingContext );
}
}
return ret;
}
@end