7
フラットファイルとしてデータを保存する前に、NSBitmapImageRep
に変換するCALayer(containerLayer
)があります。 containerLayer
のgeometryFlipped
プロパティがYESに設定されており、これが問題の原因となっているようです。最終的に生成されるPNGファイルはコンテンツを正しくレンダリングしますが、反転したジオメトリを考慮していないようです。私は明らかに左に示されている内容を正確に表すためにtest.pngを探しています。geometryFlippedでCALayerのrenderInContext:メソッドを使用する
以下に、問題と私が作業しているコードのスクリーンショットを添付します。参考のため
- (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;
}
、ここで実際に発生したNSBitmapImageRep
を節約するコードは次のとおりです。
NSData *imageData = [imageRep representationUsingType:NSPNGFileType properties:nil];
[imageData writeToFile:@"test.png" atomically:NO];