如何渲染内存中的UIViewController视图Landscape?

时间:2010-04-10 22:28:26

标签: iphone xcode ipad uiviewcontroller

我正在尝试在内存中(但不是在层次结构中)将UIViewController的视图渲染到内存中的图像缓冲区中,这样我就可以做一些有趣的过渡动画。但是,当我将UIViewController的视图渲染到该缓冲区时,无论应用程序其余部分的方向如何,它总是呈现为控制器处于纵向方向。我如何理解这个控制器?

我在RootViewController中的代码如下所示:

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];

此时,即使窗口处于横向,屏幕外存储器缓冲内容也是纵向的。

想法?

1 个答案:

答案 0 :(得分:1)

MyUIViewController如何列出其景观方向的子视图?

如果你只依赖于自动调整大小的掩码,你可以直接设置controller.view.bounds = CGRectMake(480,300)(或iPad的相应矩形),然后再渲染界面方向是否为横向。

如果有额外的布局代码(例如,在-[MyUIViewController willAnimateRotationToInterfaceOrientation:duration:]中),那么您需要确保在渲染之前调用它。