(UIImage *)screenshot_1
{
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
(UIImage *)screenshot_2
{
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0);
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
然后在 ios 7.0 中:
CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
UIImage *image = [self screenshot_1];
CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
NSLog(@"cost : %f ms", (end - start) * 1000);
然后打印:成本:10.128021 ms
CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
UIImage *image = [self screenshot_2];
CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
NSLog(@"cost : %f ms", (end - start) * 1000);
然后打印:cost:68.924010 ms
我的问题是哪个快?
最佳答案
因为 10ms < 68ms 第一个显然更快 但是请注意,第一种方法不会应用您的 View 可能具有的某些转换,而第二种方法会。 如果您计划大量使用这些功能,您应该找到一种方法来重用共享的支持上下文。
关于ios - 快速截图renderInContext : drawViewHierarchyInRect:afterScreenUpdates:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24243691/