【问题标题】:Capture a part of screen takes time捕捉屏幕的一部分需要时间
【发布时间】:2018-07-04 09:04:06
【问题描述】:

我正在使用此代码来捕获iPad 屏幕的一部分。

UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect rect = CGRectMake(500, 500, 600, 600);
CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], 
rect);
UIImage *img = [UIImage imageWithCGImage:imageRef]; 
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil); 
CGImageRelease(imageRef);

但这与使用代码捕获从 {0,0} 到 {100,100} 的部分屏幕相比非常慢

CGRect rect = CGRectMake(0, 0, 200, 200);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
[yourView.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

它们之间的区别在于传递给 UIGraphicsBeginImageContext 的矩形大小。我们可以在不将完整的屏幕边界传递给 GraphicContext 的情况下捕获 CGRectMake(500, 500, 600, 600) 吗? 请也写代码。

【问题讨论】:

  • 作为建议,您是否尝试过让这个过程在另一个队列而不是 UI(主)队列上完成?
  • 没有。由于我在每个 for 循环中更新 UI 并再次捕获,我认为转移到其他线程不会起作用。虽然我没试过,但我猜到了。

标签: ios objective-c uiimage core-graphics


【解决方案1】:

当然,您可以只捕获视图的一小部分。创建大小为 600x600 的上下文,然后将上下文的原点转换为 500,500,然后请求图层进行渲染。

CGRect rect = CGRectMake(500, 500, 600, 600);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, rect.origin.x, rect.origin.y);
[yourView.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

您可能还想查看-[UIView drawViewHierarchyInRect:afterScreenUpdates:] 方法。 According to WWDC 2013 Session 226, “Implementing Engaging UI on iOS”drawViewHierarchyInRect:afterScreenUpdates: 明显快于 renderInContext:。有关速度比较,请参见幻灯片 41:在他们的示例中,旧方法为 844 毫秒,新方法为 145 毫秒。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    • 2014-02-09
    相关资源
    最近更新 更多