【发布时间】: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