ios - 快速截图renderInContext : drawViewHierarchyInRect:afterScreenUpdates:

标签 ios ios7 screenshot

 (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/

相关文章:

javascript - React-Native:虽然前面有声明,但 undefined 不是对象

ios - 作为用户类型,在 Swift 中使用 NSNumberFormatter 扩展格式化文本字段

ios - 将应用程序升级到 iOS 7 - 错误 "Auto Layout on iOS Versions prior to 6.0"(但不希望 iOS 7 更旧)

screenshot - 使用 QTP 自动化 Greenshot

swift - View 中没有工具栏的屏幕截图

ios - 3DES 解密有时会有所不同

ios - 在 iOS 中,使字体大小适用于所有设备大小的最佳做法是什么?

ios - 显示窗口级别的 View UIWindowLevelStatusBar 退出当前的第一响应者

ios - 调用 cancelByProducingResumeData : 时,恢复数据始终为零

linux - DIY : Project remove VGA cable by sharing screen over wifi to TV