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 7545057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:49:53+00:00 2026-05-30T08:49:53+00:00

I am using CoreGraphics to have a CGLayerRef that I store into a variable.

  • 0

I am using CoreGraphics to have a CGLayerRef that I store into a variable.

I draw paths into the CGLayerRef offscreen and then when I need, I render it. This works great because I am drawing lots of paths and this helps with the performance.

The problem occurs when the iPad’s orientation changes. When this happens I need to keep my my drawings in the CGLayerRef without any changes, despite the fact that the frame changed. I don’t want to redraw everything inside of it, because it will be very slow.

So… how will I keep it as it was in the previous orientation?

To sum it up in pictures:

This is my normal portrait orientation:
enter image description here

And when the orientation switches my goal is that it will look something like this:
enter image description here

Thanks in advance,

-David

  • 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-30T08:49:54+00:00Added an answer on May 30, 2026 at 8:49 am

    It sounds like you want a view that always draws its contents in the same orientation relative to the device, regardless of the orientation of the interface (status bar, etc.). So in your images, if “hello” is drawn with the “h” by the physical volume buttons, then you always want the “h” drawn by the physical volume buttons, even if the user has rotated the device (and the rest of the UI has rotated).

    iOS rotates the user interface by setting the transform of your root view controller’s view. You can check this in the debugger:

    # Unrotated
    (lldb) po [[(id)UIApp keyWindow] recursiveDescription]
    (id) $1 = 0x0b535030 <UIWindow: 0xd434a80; frame = (0 0; 320 480); layer = <UIWindowLayer: 0xd434b70>>
       | <UIView: 0xb5385e0; frame = (0 20; 320 460); autoresize = W+H; layer = <CALayer: 0xb538710>>
    
    # Rotated with home button at the left
    (lldb) po [[(id)UIApp keyWindow] recursiveDescription]
    (id) $2 = 0x0f036060 <UIWindow: 0xd434a80; frame = (0 0; 320 480); layer = <UIWindowLayer: 0xd434b70>>
       | <UIView: 0xb5385e0; frame = (20 0; 300 480); transform = [0, -1, 1, 0, 0, 0]; autoresize = W+H; layer = <CALayer: 0xb538710>>
    

    So you can undo this by applying the inverse transform to your graphics context before drawing your content. But you have to adjust the origin of your graphics context too, which makes it a bit trickier.

    First, you need to move the origin to the center of the view. Then you apply the inverse transform to undo the interface rotation. Then you move the origin back. Assuming your CGLayer is the same size as your view (in portrait orientation), this should work:

    - (void)drawRect:(CGRect)rect
    {
        if (!_cgLayer) {
            [self initCGLayer];
        }
    
        CGContextRef gc = UIGraphicsGetCurrentContext();
        CGContextSaveGState(gc); {
            CGSize mySize = self.bounds.size;
            CGSize cgLayerSize = CGLayerGetSize(_cgLayer);
            CGContextTranslateCTM(gc, mySize.width / 2, mySize.height / 2);
            CGContextConcatCTM(gc, CGAffineTransformInvert(self.window.rootViewController.view.transform));
            CGContextTranslateCTM(gc, -cgLayerSize.width / 2, -cgLayerSize.height / 2);
            CGContextDrawLayerAtPoint(gc, CGPointZero, _cgLayer);
        } CGContextRestoreGState(gc);
    }
    

    It’s not perfect. It ends up drawing as I said: always in the same orientation relative to the device. However, because of the way interface rotation works, when the user rotates the device, it redraws the view rotated 90 degrees, and the rotates the view into the correct orientation.

    I have put a test project here:

    https://github.com/mayoff/stackoverflow-cglayer-unrotating

    In the test project, you can see a translucent toolbar that acts normally – it rotates to the edge of the screen nearest the ground. Under that is the view that uses the drawRect: method above to compensate for the user interface rotation.

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

Sidebar

Related Questions

This had been driving me crazy.... I have an iPhone app that is using
I have a little method that does some CoreGraphics drawing. Today I was using
I'm using this sniplet to center strings that are drawn with CoreGraphics. I can't
I am using CoreGraphics to draw a quadratic bezier but want to computer the
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
Is it possible to animate something that you have drawn using core graphics in
This should be a simple one, basically I have a few paths drawn with
How to draw text on imageview using coregraphics in iphone
Just wondering how to draw an ellipse using CoreGraphics.
How to draw rectangle something like this in iphone sdk using core graphics

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.