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

查看:11
本文介绍了将 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
  2. 从视图控制器获取视图并从中渲染图像
  3. 将其保存到磁盘并稍后在屏幕上显示

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

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();

尝试访问磁贴 (EXC_BAD_ACCESS) 上的 .view 属性时发生崩溃.重点是将视图渲染为背景中的图像,以防止锁定 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天全站免登陆