How can I render a UIViewController view with in-memory memory?
I'm trying to map in memory (but not in a hierarchy yet) a UIViewController view in an in-memory image buffer so I can do some interesting transition animations. However, when I render the UIViewController view in this buffer, it always renders as if the controller were in portrait orientation, regardless of the orientation of the rest of the application. How can I define this controller?
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 this point, the contents of the inactive buffers are portrait oriented, even if the window is in landscape orientation.
Ideas?
source to share
How does he MyUIViewController
lay out his views in the form of a landscape for landscape orientation?
If you only depend on autoresist masks, you can simply set controller.view.bounds = CGRectMake(480,300)
(or the corresponding rectangle for iPad) before rendering if the UI orientation is landscape.
If there is additional layout code (for example in -[MyUIViewController willAnimateRotationToInterfaceOrientation:duration:]
), you need to make sure that it is called before rendering.
source to share