2010-05-05 3 views
3

많은 CALayers가 포함 된 NSView가 있습니다. 사용자가 문서를 편집 할 때이 CALayers는 모든 편집 내용을 애니메이션으로 표시합니다. 내 애플 리케이션에 대한 인쇄를 구현하려고하지만, 이러한 CALayers를 올바르게 인쇄하는 데 문제가 있습니다.인쇄 CALayers

일부 CALayers 경계는 절대 위치가 변경되지 않기 때문에 전체 NSView를 차지하며 배치 할 필요가 없습니다. 그러나, 나는 또한 약 20 개의 작은 CALayers를 포함하는 하나의 CALayer를 가지고 있습니다. 이 CALayers는 일반적인 편집 중에 위치 변경 사항을 애니메이션으로 표시합니다. 그러나 NSView를 인쇄하려고 할 때 이러한 작은 CALayers가 올바르게 배치되지 않습니다. NSView가 정확하게 그려 지도록하기 위해해야 ​​할 특별한 것이 있는지 궁금하다.

누구나 코어 애니메이션 백엔드 NSView 인쇄 경험이 있습니까? 모든 제안을 부탁드립니다.

답변

5

-renderInContext:을 사용하여 레이어 계층을 그리는 것이 벡터 요소를 보존하지 않는다는 점과 레이아웃 문제를 해결하기 위해 Core Plot framework에 CALayer를 서브 클래 싱했습니다. CPLayer 하위 클래스는 기본 -drawInContext: 메서드를 재정의하여 사용자 정의 -renderAsVectorInContext: 메서드 (레이어의 모든 Core Graphics 드로잉을 처리하는 곳)를 호출합니다. 인쇄용 PDF 콘텍스트 (또는 유사한)를 생성하기 위해, 우리는 다음의 코드로 정의 메소드를 호출

-(void)recursivelyRenderInContext:(CGContextRef)context 
{ 
    // render self 
    CGContextSaveGState(context); 

    [self applyTransform:self.transform toContext:context]; 

    self.renderingRecursively = YES; 
    if (!self.masksToBounds) { 
     CGContextSaveGState(context); 
    } 
    [self renderAsVectorInContext:context]; 
    if (!self.masksToBounds) { 
     CGContextRestoreGState(context); 
    } 
    self.renderingRecursively = NO; 

    // render sublayers 
    for (CALayer *currentSublayer in self.sublayers) { 
     CGContextSaveGState(context); 

     // Shift origin of context to match starting coordinate of sublayer 
     CGPoint currentSublayerFrameOrigin = currentSublayer.frame.origin; 
     CGRect currentSublayerBounds = currentSublayer.bounds; 
     CGContextTranslateCTM(context, 
           currentSublayerFrameOrigin.x - currentSublayerBounds.origin.x, 
           currentSublayerFrameOrigin.y - currentSublayerBounds.origin.y); 
     [self applyTransform:self.sublayerTransform toContext:context]; 
     if ([currentSublayer isKindOfClass:[CPLayer class]]) { 
      [(CPLayer *)currentSublayer recursivelyRenderInContext:context]; 
     } else { 
      if (self.masksToBounds) { 
       CGContextClipToRect(context, currentSublayer.bounds); 
      } 
      [currentSublayer drawInContext:context]; 
     } 
     CGContextRestoreGState(context); 
    } 
    CGContextRestoreGState(context); 
} 

이 통과하고 보존 평면 코어 Graphics 문맥 상에 각 층을 렌더링 위치, 회전 및 다른 모든 변환은 날카로운 벡터로 렌더링합니다.

레이어를 렌더링 할 때주의해야 할 점은 프레젠테이션 레이어 계층 구조의 상태가 내부 레이어 계층 구조와 다를 수 있습니다. 레이어를 이동하는 데 적용된 애니메이션이있을 수 있지만 레이어의 position 속성이 일치하도록 변경되지 않았을 수 있습니다. 이 경우 값이 항상 동기화되도록 속성 자체를 애니메이션화하거나 애니메이션이 완료되면 레이어의 값을 설정해야합니다.

0

마지막으로 살펴본 결과 CALayers를 제대로 인쇄 할 수 없었습니다. 당시 Core Animation은 인쇄물이 아닌 화면 용으로 설계된 것 같았습니다 (이는 iPhone 용으로 처음 설계되었습니다).

내가 틀렸음을 알고 싶습니다.

관련 문제