捕获屏幕的一部分需要时间

时间:2018-01-25 07:08:11

标签: ios objective-c uiimage core-graphics

我正在使用此代码捕获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); 屏幕的一部分。

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

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

Sub addCC()
dim mail as Outlook.MailItem
dim recip as Recipient
set mail = Application.ActiveInspector.CurrentItem
set recip = mail.Recipients.Add("myself@mydomain.com")
recip.Type = olCC
End Sub

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

1 个答案:

答案 0 :(得分:3)

当然,您只能抓取视图的一小部分。创建大小为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 ms,示例中较新方法为145 ms。