从 UIImagePickerController 传递图像数据以进行后台处理
我正在使用 UIImagePickerController 来捕获静态图像。然后,我需要在将图像的不同副本保存到核心数据存储之前进行一些处理工作。在 iPhone 4 上,处理和保存工作可能需要长达 4-8 秒的时间,因此我尝试将工作分支到后台队列,以便整个应用程序和 UI 不会阻塞。
我的问题的根源是这样的。只要 UIImage 对象完全限制在该线程中,是否可以在后台线程中使用 UIImage?我在 苹果关于 NSImage 的线程安全总结。我假设 UIImage 也会以同样的方式工作。
NSImage 限制:
一个线程可以创建一个 NSImage 对象,绘制到图像缓冲区,并将其传递给主线程进行绘制。底层图像缓存在所有线程之间共享。有关图像以及缓存如何工作的更多信息,请参阅 Cocoa 绘图指南。
任何人都可以确认这一点,还是在主线程之外触摸 UIImage 对象之类的东西是完全错误的。如果使用受限的 UIImage 实例是可以的,那么这会导致另一个问题。 UIImagePickerController 返回一个线程安全的 NSDictionary,但在该 NSDictionary 中是一个 UIImage 对象。将该字典传递给另一个线程然后在该线程中使用包含的对象是否安全?
如果 imagePicker 信息字典中的 UIImage 使用不安全,那么关于如何最好地继续进行有什么建议吗?
我想我已经解决了实际的核心数据线程问题。但就信息而言,我目前使用 NSValueTransformer 编写和检索图像数据,以在自定义 NSManagedObject 子类中将 UIImage 与 NSData 相互转换。
I'm using a UIImagePickerController to capture a still image. I then need to do some processing work before saving different copies of the image to a Core Data store. The processing and saving work can take up to 4-8 seconds on an iPhone 4 so I'm trying to branch the work off to a background queue so the whole app and UI doesn't block.
At the root of my question is this. Is it possible to use a UIImage in a background thread so long as the UIImage object is totally confined to that thread? I found the following in apple's thread safety summary about NSImage. I'm assuming UIImage would work the same way.
NSImage Restrictions:
One thread can create an NSImage object, draw to the image buffer, and pass it off to the main thread for drawing. The underlying image cache is shared among all threads. For more information about images and how caching works, see Cocoa Drawing Guide.
Can anyone confirm this or is it just plain wrong to touch something like a UIImage object outside of the main thread. If using a confined UIImage instance is okay, then I that leads to another issue. The UIImagePickerController returns an NSDictionary which is thread safe, but within that NSDictionary is a UIImage object. Is it safe to pass that dictionary off to another thread then use the contained object to within that thread?
If the UIImage in the imagePicker info dictionary is not safe to use then any suggestions on how best to proceed?
I think I have the actual Core Data threading issues figured out. But for information I currently write and retrieve the image data using an NSValueTransformer to transform a UIImage to and from NSData within a custom NSManagedObject subclass.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在后台线程中处理了一些“在 UIImagePickerController 之后处理图像”代码,就像您一样,没有任何问题。以下是我成功的步骤。
I worked on some "Process an image after UIImagePickerController" code in a background thread just as you are and had no problems. Below are the steps I had success with.