Make UIImage From UIView but NOT in the main threa

2019-02-23 14:50发布

问题:

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!

回答1:

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