从UIView制作UIImage,但不在主线程中 [英] Make UIImage From UIView but NOT in the main thread

查看:65
本文介绍了从UIView制作UIImage,但不在主线程中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用众所周知的模式从UIView创建UIImage:

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;
}

现在,我的问题是我有一个非常复杂的视图,上面有很多子视图,因此转换过程大约需要3 +(!!!)秒.

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.

唯一的问题是,我记得,不允许在主线程中制作与UI相关的东西.

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?

非常感谢!

推荐答案

最后,我只是在另一个线程中做到了,一切都很好.

In the end i just did it in another thread, and everything is working fine.

这篇关于从UIView制作UIImage,但不在主线程中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆