Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 790961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:45:31+00:00 2026-05-14T21:45:31+00:00

I have a NSView which contains many CALayers. When a user is editing a

  • 0

I have a NSView which contains many CALayers. When a user is editing a document, these CALayers animate all edits. I am attempting to implement printing for my app, but I am having some problems printing these CALayers correctly.

Some CALayers bounds occupy the entire NSView, and do not need to be laid out, because their position never changes. However, I also have one CALayer which contains about 20 small CALayers. These CALayers animate their position changes during normal editing. However, when attempting to print the NSView, these small CALayers never get laid out correctly. I am wondering if there is something special I have to do to ensure that these layers are positioned correctly, and allow the NSView to be drawn/printed correctly.

Does anyone have experience printing a Core Animation backed NSView? Any suggestions are appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T21:45:32+00:00Added an answer on May 14, 2026 at 9:45 pm

    In order to work around layout issues, as well as the fact that using -renderInContext: to draw a layer hierarchy does not preserve vector elements, we subclassed CALayer in the Core Plot framework. The CPLayer subclass overrides the default -drawInContext: method to call our custom -renderAsVectorInContext: method (where we do all of our Core Graphics drawing for a layer). To generate a PDF context (or similar) for printing, we then call a custom method with the following code:

    -(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);
    }
    

    This goes through and renders each layer onto a flat Core Graphics context, preserving position, rotation, and other transforms while rendering all elements as sharp vectors.

    One other thing to watch out for when trying to render layers is that the state of your presentation layer hierarchy may not be the same as your internal layer hierarchy. You may have animations that have been applied to move your layers, but the layers’ position properties may not have been changed to match. In that case, you should make sure that you either animate the properties themselves, so that the values always stay in sync, or set the values in your layer once the animations have completed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.