NSView
을 사용하여 여러 코어 애니메이션 CALayer
개체를 호스팅하고 있습니다. 내가 할 수 있기를 원하는 것은 뷰의 현재 상태에 대한 스냅 샷을 비트 맵 이미지로 가져 오는 것입니다.Core Animation 레이어가 포함 된 뷰를 비트 맵에 렌더링하려면 어떻게해야합니까?
이이 같은 정상 NSView
사용하는 것이 상대적으로 간단하다 그러나
void ClearBitmapImageRep(NSBitmapImageRep* bitmap) {
unsigned char* bitmapData = [bitmap bitmapData];
if (bitmapData != NULL)
bzero(bitmapData, [bitmap bytesPerRow] * [bitmap pixelsHigh]);
}
@implementation NSView (Additions)
- (NSBitmapImageRep*)bitmapImageRepInRect:(NSRect)rect
{
NSBitmapImageRep* imageRep = [self bitmapImageRepForCachingDisplayInRect:rect];
ClearBitmapImageRep(imageRep);
[self cacheDisplayInRect:rect toBitmapImageRep:imageRep];
return imageRep;
}
@end
,이 코드를 사용할 때, 코어 애니메이션 레이어는 렌더링되지 않습니다.
나는 내가 필요한 것을하는 것으로 보였으므로 CARenderer
을 조사했지만 기존 계층 트리를 렌더링 할 수는 없습니다.
CAContextInvalidLayer -- layer <CALayer: 0x1092ea0> is already attached to a context
나는 레이어 트리 내 NSView
에서 호스팅하고 있기 때문에이 있어야합니다 같은데요 : 나는이 작업을 수행 할 때이 예외가, 그러나
NSOpenGLPixelFormatAttribute att[] =
{
NSOpenGLPFAWindow,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAColorSize, 24,
NSOpenGLPFAAlphaSize, 8,
NSOpenGLPFADepthSize, 24,
NSOpenGLPFANoRecovery,
NSOpenGLPFAAccelerated,
0
};
NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:att];
NSOpenGLView* openGLView = [[NSOpenGLView alloc] initWithFrame:[self frame] pixelFormat:pixelFormat];
NSOpenGLContext* oglctx = [openGLView openGLContext];
CARenderer* renderer = [CARenderer rendererWithCGLContext:[oglctx CGLContextObj] options:nil];
renderer.layer = myContentLayer;
[renderer render];
NSBitmapImageRep* bitmap = [oglView bitmapImageRepInRect:[oglView bounds]];
: 나는 다음과 같은 시도 그러므로 그것의 맥락에 붙어있다. 비트 맵으로 렌더링하기 위해 NSView
에서 레이어 트리를 분리 할 수있는 방법을 이해할 수 없으며이 경우에는 중복 레이어 트리를 만드는 것이 중요하지 않습니다.
CALayer
을 비트 맵으로 렌더링하는 다른 방법이 있습니까? 이 일을 위해 어디에서나 샘플 코드를 찾을 수 없습니다. 실제로 CARenderer
에 대한 샘플 코드를 찾을 수 없습니다.
많은 분들께 감사 드리며, 이것이 내가 필요로하고 완벽하게 작동하는 것입니다. 앞에서'-renderInContext :'메소드를 발견했으면 좋겠다.'CARENDerer'가 정원 경로를 이끌었다. –
요소에 대해이 작업이 오프 스크린입니까? 예 : 내 CALayer에는 아직 뷰 프레임에없는 요소가 있지만 스크롤해야 볼 수 있습니다. – Tudorizer
누구나 [UIView animateWithDuration : ...]으로 시작된 애니메이션 작업에이 작업을 얻었습니까? 그것은 나를 위해 작동하지 않았다. – Isak