iPad详细信息视图方向错误的屏幕截图

时间:2013-12-06 12:07:51

标签: ipad screenshot xcode5

我正在尝试在iPad上捕捉横向主/详细布局中的详细视图的屏幕截图。 这是我尝试使用的代码。

UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
CGRect rect = [self.view bounds];
UIGraphicsBeginImageContextWithOptions(rect.size, YES, 0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[keyWindow.layer renderInContext:context];
UIImage *capturedScreen = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

这有两个问题。   - 屏幕捕获方向不正确。我得到的图像就在它的旁边。   - 宽度= 703&屏幕截图反转高度= 768尺寸,因此我最终在细节屏幕截图中显示了一些主视图。 我在这做错了什么?谢谢!

2 个答案:

答案 0 :(得分:1)

试试这种方式

-(UIImage *)captureScreenForRect:(CGRect)frame
{       
    CALayer *layer;
    layer = self.view.layer;
    UIGraphicsBeginImageContext(self.view.bounds.size);
    CGContextClipToRect (UIGraphicsGetCurrentContext(),self.view.bounds);
    [layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return screenImage;
}

传递您的详细视图框架rect以获取上述方法。希望这会对你有所帮助

“官方”截图方法如下: (https://developer.apple.com/library/ios/qa/qa1703/_index.html

答案 1 :(得分:1)

如果你需要这个用于转换或图形效果以及使用iOS 7 - 我建议你不要实际创建图像。 生成的图像可能很重(例如在iPAd Retina 3rd Gen上)并且在内存上很重。

启动iOS 7 Apple在UIView上提供了更快的快照功能(顺便说一下,它们也是在视图控制器中实现自定义转换的方式,模糊效果等),这比创建实际图像要快得多。

在UIView上你可以执行: - (UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates

或者,如果您需要模糊视图的完整视图层次结构:

- (BOOL)drawViewHierarchyInRect:(CGRect)rect afterScreenUpdates:(BOOL)afterUpdates