使用 CALayer
进行绘图,然后通过 renderInContext
和 UIGraphicsGetImageFromCurrentImageContext()
生成图片这一系列的操作可以在非主线程吗?(并非操作显示在View上的Layer)
UI操作还是主线程的吧
可以放在后台线程的。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),^{
CGSize constrainedSize = {100, 100};
UIGraphicsBeginImageContextWithOptions(constrainedSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
{
[self.view.layer renderInContext:context];
}
CGContextRestoreGState(context);
UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
});