我必须在某些情况下改变导航,并且由于过渡的复杂性,我已经采取了绘画和屏幕截图,直到转换完成。在大多数情况下,这很有效,但有一点让我感到不安。我有一个带有两个选择器视图的视图控制器:
但截图在这个VC上效果不佳。我明白了:
在这两种情况下,获取屏幕截图的代码如下:
- (UIImage *)takeScreenshot {
CALayer *layer = [[UIApplication sharedApplication] keyWindow].layer;
UIGraphicsBeginImageContext(layer.frame.size);
[layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
return screenshot;
}
任何人都知道怎么可能发生?
答案 0 :(得分:4)
您可以尝试使用其他方法进行屏幕截图。 Apple在iOS 7中引入了一些快速查看截图的方法。
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0);
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
UIImage *im = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Here是Apple的回答,它提供了有关这两种方法如何工作的更多信息。虽然相应的用户遇到了一些pb,并被建议使用旧的方式来快照视图,但我从来没有遇到任何问题。也许从那以后他们就修好了。
答案 1 :(得分:1)
UIGraphicsBeginImageContext(self.window.bounds.size);
[self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);
[data writeToFile:@"image.png" atomically:YES];
如果你有一个视网膜显示器,那么用下面的代码替换第一行: -
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
UIGraphicsBeginImageContextWithOptions(self.window.bounds.size, NO,[UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext(self.window.bounds.size);