도면 뷰의 계층으로는 특정 상황에서 코코아
-
08-06-2019 - |
문제
에 대한 내 응용 프로그램의 일부가 이미지를 만들어야의 특정을 보고 그것의 모든 하위.
이를 위해 나가는 컨텍스트를 만들어 랩을 비트맵으로 동일한 크기로 볼 수 있지만,나는 어떻게 그리는 계층 보기합니다.그릴 수 있는 단일을 볼 수 컨텍스트를 설정하고 명시적으로 호출 drawRect 지만,이는 다루지 않으로 모든 하위.
나는 볼 수없는 아무것도에 NSView 인터페이스를 도울 수 있는 이 그래서 나는 솔루션을 거짓말을 할 수 있습에서 더 높은 수준입니다.
해결책
내가 찾는 쓰리 코드를 자신이 가장 좋은 방법이었:
- 잠재적인 투명성 문제(의 일부 다른 옵션은 추가 흰 배경을 전체 이미지)
- 성능이 훨씬 더
아래 코드가 완벽하지 않기 때문에,그것은 처리하지 않습 scaling 문제를 갈 경우에서 경계하는 프레임지만,그 계정으로 isFlipped 상태,그리고 아주 잘 작동을 위해 내가 무엇을 사용합니다.그것은 단지 그 하위(및 subsubviews,...재귀적으로),하지만 그것도 그리기 자체가 매우 쉽고,추가 [self drawRect:[self bounds]]
의 구현에 imageWithSubviews
.
- (void)drawSubviews
{
BOOL flipped = [self isFlipped];
for ( NSView *subview in [self subviews] ) {
// changes the coordinate system so that the local coordinates of the subview (bounds) become the coordinates of the superview (frame)
// the transform assumes bounds and frame have the same size, and bounds origin is (0,0)
// handling of 'isFlipped' also probably unreliable
NSAffineTransform *transform = [NSAffineTransform transform];
if ( flipped ) {
[transform translateXBy:subview.frame.origin.x yBy:NSMaxY(subview.frame)];
[transform scaleXBy:+1.0 yBy:-1.0];
} else
[transform translateXBy:subview.frame.origin.x yBy:subview.frame.origin.y];
[transform concat];
// recursively draw the subview and sub-subviews
[subview drawRect:[subview bounds]];
[subview drawSubviews];
// reset the transform to get back a clean graphic contexts for the rest of the drawing
[transform invert];
[transform concat];
}
}
- (NSImage *)imageWithSubviews
{
NSImage *image = [[[NSImage alloc] initWithSize:[self bounds].size] autorelease];
[image lockFocus];
// it seems NSImage cannot use flipped coordinates the way NSView does (the method 'setFlipped:' does not seem to help)
// Use instead an NSAffineTransform
if ( [self isFlipped] ) {
NSAffineTransform *transform = [NSAffineTransform transform];
[transform translateXBy:0 yBy:NSMaxY(self.bounds)];
[transform scaleXBy:+1.0 yBy:-1.0];
[transform concat];
}
[self drawSubviews];
[image unlockFocus];
return image;
}
다른 팁
당신이 사용할 수 있는 -[NSView dataWithPDFInsideRect:]
를 렌더링하기 전체 보기 당신은 그것을 보낼 PDF,로 반환됩 NSData
체입니다.당신은 할 수 있습니 당신이 무엇을 원하는,포함하여 렌더링으로 그것을 비트맵.
당신은 당신이 원하는 비트맵 표현을 하지만?모든 후,그 PDF 수 있(적어도 이론)해상도의 영향을 받지 않습니다.
당신이 사용할 수 있는 -[NSBitmapImageRep initWithFocusedViewRect:]
잠금 후에 초점을 볼 수 있는 뷰를 렌더링 자체가(그리고 그것의 하위)으로 주어진 사각형입니다.
당신이 원하는 무엇이 명시적으로 사용할 수 있다."섹션을 참조하십시오 NSView 그림을 리디렉션 API"에 10.4AppKit 릴리스.
는 NSBitmapImageRep 캐시고 명확한 그것:
NSGraphicsContext *bitmapGraphicsContext = [NSGraphicsContext graphicsContextWithBitmapImageRep:cacheBitmapImageRep];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:bitmapGraphicsContext];
[[NSColor clearColor] set];
NSRectFill(NSMakeRect(0, 0, [cacheBitmapImageRep size].width, [cacheBitmapImageRep size].height));
[NSGraphicsContext restoreGraphicsState];
캐시다:
-[NSView cacheDisplayInRect:toBitmapImageRep:]
하려면 더 일반적으로 그리로 지정 컨텍스트 처리를 보 재귀과 투명성,올바르게
-[NSView displayRectIgnoringOpacity:inContext:]
그래요,이를 위해 특정 사용량 유엔-스케일 이미지 데이터가 정확히 무엇을 내가 필요합니다.