在后台线程上渲染UIVIew到图像的安全方法? [英] Safe way to render UIVIew to an image on background thread?

查看:141
本文介绍了在后台线程上渲染UIVIew到图像的安全方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建小磁贴图像以存储在磁盘上并显示给用户。以下是我目前执行此操作的过程:

I'm creating small tile images to be stored on disk and displayed to the user. Here is my current process for doing this:


  1. 创建一个代表我想在屏幕上显示的UI的viewcontroller

  2. 从视图控制器获取视图并从中渲染图像

  3. 将其保存到磁盘并稍后在屏幕上显示

当我尝试访问viewcontroller的视图时,我崩溃了。当我尝试在线研究时,我在后台创建视图是否安全时遇到了相互矛盾的结果。我正在读UIGraphicsGetCurrentContext调用应该是线程安全的,但也许不能在后台线程上访问UIView?我正在为iOS 4及更高版本编写应用程序。这是我正在使用的代码(tile是viewcontroller):

I am crashing when I attempt to access the view of the viewcontroller. When I tried to research this online, I am getting conflicting results on whether it is safe to create the view in the background. I'm reading that the UIGraphicsGetCurrentContext call should be thread safe, but maybe not accessing the UIView on a background thread? I'm writing the app for iOS 4 and above. Here is the code I'm using (tile is the viewcontroller):

CGSize size = CGSizeMake(20.0f, 30.0f);
UIGraphicsBeginImageContext(size);
[tile.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();           
UIGraphicsEndImageContext();

尝试访问磁贴上的.view属性时发生崩溃(EXC_BAD_ACCESS)。重点是在后台渲染视图以防止锁定UI,因为有很多要处理的磁贴。

The crash occurs when trying to access the .view property on the tile (EXC_BAD_ACCESS). The whole point is to render the view to an image in the background to prevent locking up the UI because there are a lot of tiles to process.

有没有一种安全的方法可以做到这一点?

Is there a safe way to do this?

推荐答案

我从未尝试过你所描述的内容,我不喜欢成为坏消息的承载者,而是从 Apple的文档

I've never tried what you described, and I don't like being the bearer of bad news, but taken from Apple's documentation.


注意:大多数情况下,UIKit类
只能从
应用程序的主线程中使用。这是
特别适用于来自UIResponder的
派生类或涉及以任何方式操纵应用程序的用户
界面的

Note: For the most part, UIKit classes should be used only from an application’s main thread. This is particularly true for classes derived from UIResponder or that involve manipulating your application’s user interface in any way.

当然,他们很方便地说大部分时间,这样就留下了一些摆动空间。

Of course, they conveniently say "For the most part", so that leaves some wiggle room.

这篇关于在后台线程上渲染UIVIew到图像的安全方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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