iOS:what';以编程方式制作屏幕截图的最快、最高效的方法是什么?
在我的iPad应用程序中,我想制作一个占据大部分屏幕的UIView屏幕截图。不幸的是,子视图嵌套得很深,因此需要很长时间才能制作屏幕截图,并在截图之后设置页面卷曲动画 有比“通常”更快的方法吗 如果可能,我希望避免缓存或重新构造视图。iOS:what';以编程方式制作屏幕截图的最快、最高效的方法是什么?,ios,performance,core-graphics,graphicscontext,quartz-core,Ios,Performance,Core Graphics,Graphicscontext,Quartz Core,在我的iPad应用程序中,我想制作一个占据大部分屏幕的UIView屏幕截图。不幸的是,子视图嵌套得很深,因此需要很长时间才能制作屏幕截图,并在截图之后设置页面卷曲动画 有比“通常”更快的方法吗 如果可能,我希望避免缓存或重新构造视图。 编辑10月3日。2013年 已更新以支持iOS 7中新的超快速drawViewHierarchyInRect:AfterScreenuUpdate:method 不,CALayer的RenderContext:据我所知,这是唯一的方法。您可以创建一个UIView
编辑10月3日。2013年 已更新以支持iOS 7中新的超快速drawViewHierarchyInRect:AfterScreenuUpdate:method
不,CALayer的RenderContext:据我所知,这是唯一的方法。您可以创建一个UIView类别,如下所示,以便于您自己继续: UIView+屏幕截图
#import <UIKit/UIKit.h>
@interface UIView (Screenshot)
- (UIImage*)imageRepresentation;
@end
要查看此问题的答案:我发现了一种更好的方法,它尽可能使用快照API 我希望有帮助
class func screenshot() -> UIImage {
var imageSize = CGSize.zero
let orientation = UIApplication.shared.statusBarOrientation
if UIInterfaceOrientationIsPortrait(orientation) {
imageSize = UIScreen.main.bounds.size
} else {
imageSize = CGSize(width: UIScreen.main.bounds.size.height, height: UIScreen.main.bounds.size.width)
}
UIGraphicsBeginImageContextWithOptions(imageSize, false, 0)
for window in UIApplication.shared.windows {
window.drawHierarchy(in: window.bounds, afterScreenUpdates: true)
}
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
目标-C版本:
+ (UIImage *)screenshot
{
CGSize imageSize = CGSizeZero;
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsPortrait(orientation)) {
imageSize = [UIScreen mainScreen].bounds.size;
} else {
imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
}
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
CGContextSaveGState(context);
CGContextTranslateCTM(context, window.center.x, window.center.y);
CGContextConcatCTM(context, window.transform);
CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y);
if (orientation == UIInterfaceOrientationLandscapeLeft) {
CGContextRotateCTM(context, M_PI_2);
CGContextTranslateCTM(context, 0, -imageSize.width);
} else if (orientation == UIInterfaceOrientationLandscapeRight) {
CGContextRotateCTM(context, -M_PI_2);
CGContextTranslateCTM(context, -imageSize.height, 0);
} else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
CGContextRotateCTM(context, M_PI);
CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);
}
if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
[window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
} else {
[window.layer renderInContext:context];
}
CGContextRestoreGState(context);
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
您要求的另一种选择是读取GPU(因为屏幕是由任意数量的半透明视图合成的),这本身也是一种缓慢的操作。iOS 7引入了一种新方法,允许您在当前图形上下文中绘制视图层次结构。这可用于快速获取
ui图像
在UIView
上作为类别方法实现:
- (UIImage *)pb_takeSnapshot {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
它比现有的renderInContext:
方法快得多
SWIFT更新:执行相同操作的扩展:
extension UIView {
func pb_takeSnapshot() -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, UIScreen.mainScreen().scale);
self.drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true)
// old style: self.layer.renderInContext(UIGraphicsGetCurrentContext())
let image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
}
对我来说,设置插值质量走了很长的路
CGContextSetInterpolationQuality(ctx, kCGInterpolationNone);
如果您正在拍摄非常详细的图像,此解决方案可能无法接受。如果你正在拍摄文本,你几乎不会注意到其中的区别
这大大缩短了拍摄快照的时间,并制作了占用内存少得多的图像
这对于drawViewHierarchyInRect:AfterCreeneUpdates:method仍然是有益的。我将对任何iOS版本(即使是视网膜或非视网膜设备)运行的单一功能的答案进行了组合
- (UIImage *)screenShot {
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext(self.view.bounds.size);
#ifdef __IPHONE_7_0
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
#endif
#else
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
#endif
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
谢谢;我现在正在使用一个类别;但我正在寻找一种更高效的方式来制作截图…:/@编辑:这就是我正在做的-我得到容器的图像表示。但这并不能帮助我解决有关性能的问题…对我来说也是如此。。。难道没有办法不重新渲染所有内容吗?没错,iOS使用内部图像表示来加快渲染速度。只有更改的视图才会重新提交。但是,如果你问如何获得内部图像表示,而不需要重新绘制,我认为这是不可能的。如上所述,此图像可能存在于GPU中,并且很可能无法通过公共API访问。我需要在屏幕更新后使用:是的
,但除此之外,它工作得很好。因此没有更快的解决方案?它不在ios上,既然gpu和cpu共享同一个RAM,该解决方案的性能是否比原始海报提供的解决方案更好?我自己的测试表明这是完全一样的。总的来说,我会选择原来的解决方案,因为代码要简单得多。@GregMaletic:是的,另一个解决方案看起来更简单,但它在UIView上工作,而这个解决方案在UIWindow上工作,所以更完整。我仍然不明白为什么这个解决方案更快。大多数iOS应用程序只包含一个窗口。不只是[self.window.layer renderInContext:context]就可以了吗?我认为这不管用。RenderContext的性能问题已经有了很好的记录,在窗口层上调用它并不能解决这个问题。在我的测试中,RenderContext的性能也比drawViewHierarchyInRect、iOS 11.2好得多。完成后别忘了调用UIGraphicsSendImageContext。您测试过它实际上更快吗?我的测试几乎没有提高性能,即使将AfterScreenUpdate设置为NO.@maxpower,我也对执行进行了计时,速度提高了50%以上。使用旧的RenderContext:大约需要0.18秒,使用它需要0.063秒。我相信您的结果会因设备中的CPU而有所不同。是我自己还是self.drawViewHierarchyInRect(self.bounds,afterscreenuupdates:true)
在执行时会导致一个奇怪的显示错误?我对self.layer.renderContext(UIGraphicsGetCurrentContext())
没有同样的问题。你能告诉我你看到了什么样的差异吗?我看到时间略有增加,但不幸的是我不能。我再也无法访问该项目了。换了工作。但是我可以说被截屏的视图在它的层次结构中大概有50+-10个视图。我还可以说,大约1/4-1/3的视图是图像视图在进一步研究中,唯一一次我看到插值设置有任何差异的时候,是在渲染视图时调整视图大小,还是渲染到更小的上下文中。我假设这主要取决于所讨论的特定上下文。至少还有一个人从中看到了显著的结果。请参阅对此答案的评论。
extension UIView {
func pb_takeSnapshot() -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, UIScreen.mainScreen().scale);
self.drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true)
// old style: self.layer.renderInContext(UIGraphicsGetCurrentContext())
let image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
}
CGContextSetInterpolationQuality(ctx, kCGInterpolationNone);
- (UIImage *)screenShot {
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext(self.view.bounds.size);
#ifdef __IPHONE_7_0
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
#endif
#else
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
#endif
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}