I modified Create an on-memory CGImageRef to draw the small blue rectangle on a CGLayer, and combines the CGLayer to the background graphics context to generate the image. The result should remain same.
const int height = 250;
const int width = 400;
CGColorSpaceRef imageColorSpace = CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB );
CGContextRef context = CGBitmapContextCreate (NULL, width, height,
8, width * 4,
imageColorSpace, kCGImageAlphaPremultipliedLast);
CGContextSetRGBFillColor (context, 1.0, 0.8, 0.8, 1);
CGContextFillRect (context, CGRectMake (0, 0, width, height ));
// Addition starts here.
CGLayerRef layer;
CGContextRef layerContext;
layer = CGLayerCreateWithContext(context,CGSizeMake(width, height),NULL);
layerContext = CGLayerGetContext(layer);
CGContextSetRGBFillColor(layerContext, 0, 0, 1, 1);
CGContextFillRect (layerContext, CGRectMake (80, 80, 40, 30));
CGContextDrawLayerInRect(context, CGRectMake(0, 0, width, height), layer);
// Addition finished.
CGImageRef image = CGBitmapContextCreateImage(context);
[imageView setImage:image imageProperties:nil];
CGColorSpaceRelease( imageColorSpace );
CGLayerRelease(layer);
CGContextRelease(layerContext);
CGContextRelease(context);
CGImageRelease( image );


