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

Update는이 귀하의 코드는, 난 그냥 같은 문제를 해결 한