views:

140

answers:

1

I'm trying to render an in-memory (but not in hierarchy, yet) UIViewController's view into an in-memory image buffer so I can do some interesting transition animations. However, when I render the UIViewController's view into that buffer, it is always rendering as though the controller is in Portrait orientation, no matter the orientation of the rest of the app. How do I clue this controller in?

My code in RootViewController looks like this:

MyUIViewController* controller = [[MyUIViewController alloc] init];
int width = self.view.frame.size.width;
int height = self.view.frame.size.height;
int bitmapBytesPerRow = width * 4;

unsigned char *offscreenData = calloc(bitmapBytesPerRow * height, sizeof(unsigned char)); 

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef offscreenContext = CGBitmapContextCreate(offscreenData, width, height, 8, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast); 
CGContextTranslateCTM(offscreenContext, 0.0f, height);
CGContextScaleCTM(offscreenContext, 1.0f, -1.0f);

[(CALayer*)[controller.view layer] renderInContext:offscreenContext];

At that point, the offscreen memory buffers contents are portrait-oriented, even when the window is in landscape orientation.

Ideas?

A: 

How does MyUIViewController lay out its view's subviews for the landscape orientation?

If you're depending only on autoresizing masks, you could just directly set controller.view.bounds = CGRectMake(480,300) (or the appropriate rect for iPad) before rendering if the interface orientation is landscape.

If there's additional layout code (e.g., in -[MyUIViewController willAnimateRotationToInterfaceOrientation:duration:]), then you'll need to make sure that gets called before rendering as well.

Tom
Worked great! Here's the code I added:UIInterfaceOrientation orientation = [UIDevice currentDevice].orientation;if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight){ controller.view.bounds = CGRectMake(0, 0, 1024,768);}
Aaron