I have a CALayer ( containerLayer ) that I want to convert to NSBitmapImageRep before saving the data as a flat file. containerLayer has the geometryFlipped property set to YES and this seems to be causing problems. The PNG file that is ultimately generated displays the content correctly, but does not seem to take into account the changed geometry. I'm explicitly looking for test.png to accurately represent the content shown on the left.
Below is a screenshot of the problem and the code I'm working with.

- (NSBitmapImageRep *)exportToImageRep { CGContextRef context = NULL; CGColorSpaceRef colorSpace; int bitmapByteCount; int bitmapBytesPerRow; int pixelsHigh = (int)[[self containerLayer] bounds].size.height; int pixelsWide = (int)[[self containerLayer] bounds].size.width; bitmapBytesPerRow = (pixelsWide * 4); bitmapByteCount = (bitmapBytesPerRow * pixelsHigh); colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); context = CGBitmapContextCreate (NULL, pixelsWide, pixelsHigh, 8, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast); if (context == NULL) { NSLog(@"Failed to create context."); return nil; } CGColorSpaceRelease(colorSpace); [[[self containerLayer] presentationLayer] renderInContext:context]; CGImageRef img = CGBitmapContextCreateImage(context); NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithCGImage:img]; CFRelease(img); return bitmap; }
For reference, here is the code that actually stores the generated NSBitmapImageRep :
NSData *imageData = [imageRep representationUsingType:NSPNGFileType properties:nil]; [imageData writeToFile:@"test.png" atomically:NO];
ndg
source share