以编程方式截取屏幕截图非常慢

时间:2016-07-09 13:56:16

标签: ios objective-c

我有以下代码,每次加载大约需要7-10秒,我想优化它,这样就可以在一秒或更短时间内共享屏幕截图和负载。

UIImage *imageToShare = [self screenshot];
NSArray *activityItems = @[stringToShare, imageToShare];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityVC.excludedActivityTypes = @[UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypePostToWeibo];
[self presentViewController:activityVC animated:TRUE completion:nil];

- (UIImage *) screenshot {
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);

    [self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

我不太确定如何让这个跑得更快?有什么想法吗?

1 个答案:

答案 0 :(得分:0)

据我所知drawViewHierarchyInRect: afterScreenUpdates:很重。我总是使用renderInContext:对视图进行快照:

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();