2011-10-21 3 views
7

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

以下に、問題と私が作業しているコードのスクリーンショットを添付します。参考のため

A visual example

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

答えて

6

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

CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, pixelsHigh); 
CGContextConcatCTM(context, flipVertical); 
[[[self containerLayer] presentationLayer] renderInContext:context]; 

更新はこれで、あなたのコードは、私はちょうど同じ問題を解決しました

関連する問題