快速截图renderInContext:drawViewHierarchyInRect:afterScreenUpdates:

时间:2014-06-16 12:25:13

标签: 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毫秒

CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();   
UIImage *image = [self screenshot_2];  
CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();  
NSLog(@"cost : %f ms", (end - start) * 1000);  

然后打印:费用:68.924010毫秒

我的问题是哪个快?

1 个答案:

答案 0 :(得分:1)

从10ms开始< 68ms第一个显然更快 但要注意,第一种方法不会应用您的视图可能具有的某些转换,而第二种方法则不会。 如果您计划大量使用这些功能,您应该找到一种重用共享支持上下文的方法。