从 UIView 制作 UIImage 但不在主线程中
我正在使用众所周知的模式从 UIView
创建 UIImage
:
+ (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, [[UIScreen mainScreen] scale]);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
现在,我的问题是我有一个非常复杂的视图,上面有很多子视图,所以这个转换过程大约需要3+(!!!)秒。
我尝试将其分叉到另一个在后台运行的线程中,它确实提高了性能。
唯一的问题是,据我所知,不允许在主线程之外制作与 UI 相关的内容。
我错了吗?这完全没问题吗? 或者 - 如果我是对的 - 可以采取什么措施来提高性能?有没有其他方法可以在不同的线程中使用但效果相同?
多谢!
I'm using the well-known pattern to create an UIImage
from an UIView
:
+ (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, [[UIScreen mainScreen] scale]);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
Now, my problem is that i have a very complex view with a lot of subviews on it, so this process of conversion takes about 3+(!!!) seconds.
I tried forking it into another thread that run in the background and it really did improve the performance.
The only problem is that as can I remember, it is not allowed to make UI related stuff not in the main thread.
Am I wrong and this is perfectly fine?
Or - if i'm right - what can be done to improve performance? is there any other method that i can use in a different thread but does the same work?
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后我只是在另一个线程中完成了它,一切都工作正常。
In the end i just did it in another thread, and everything is working fine.