7

データをフラットファイルとして保存containerLayerする前に、に変換しようとしている CALayer ( ) があります。プロパティが YES に設定されており、これが問題を引き起こしているようです。最終的に生成される PNG ファイルは、コンテンツを正しくレンダリングしますが、反転したジオメトリを考慮していないようです。左に表示されているコンテンツを正確に表すために、明らかに test.png を探しています。NSBitmapImageRepcontainerLayergeometryFlipped

以下に添付されているのは、問題のスクリーンショットと私が使用しているコードです。

視覚的な例

- (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]; 
4

1 に答える 1

6

レンダリングするに、宛先コンテキストを反転する必要があります。

これでコードを更新してください。同じ問題を解決しました:

CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, pixelsHigh);
CGContextConcatCTM(context, flipVertical);
[[[self containerLayer] presentationLayer] renderInContext:context];
于 2012-01-03T19:01:46.927 に答える