UIImage Thread Safety

I know that Apple officially recommends using UIKit only in the main thread. However, I also heard allegations that UIImage is thread safe with iOS 4.0. I can’t find any documentation supporting this claim.

Does anyone have any information to support this requirement? As a class used for storing data and decoding image data, UIImage should be thread safe if it is well designed.

+3
source share
6 answers

True, Apple recommends using elements from UIKIt in the main thread:

Note. For the most part, UIKit classes should only be used with the main application thread. This is especially true for classes derived from UIResponder, or which are related to managing your applications.

Since UIImage is not derived from a UIResponder, you are not actually displaying it on an interface / screen. Then, operations with UIImages on another thread should be safe.

This, however, is based on my experience; I have not seen official documentation about this.

+5
source

Directly from Apple documentation for UIImage

Image objects are immutable, so you cannot change their properties after creation. This means that you usually specify image properties during initialization or rely on image metadata to provide a property value. It also means that image objects themselves are safe to use from any stream. . As you change the properties of an existing image object, use one of the available convenience methods to create a copy of the image, but with the custom value that you want.

(Emphasis mine)

So, at least in the current version of the SDK dated May 13, 2014, "image objects themselves are safe to use from any stream."

+25
source

In the What New release notes on iOS: iOS 4.0, UIKit Framework enhancements include this bit:

Drawing graphical context in UIKit is now thread safe. In particular: the routines used to access and manage the chart context can now correctly handle contexts located on different threads. The line and drawing pattern are now thread safe. Using colors and font objects in multiple threads is now safe.

So UIImage is thread safe on iOS 4.0 and later.

+2
source

Apple appears to have updated its documentation as earlier answers were posted earlier. According to the latest documentation, you can create and use UIImage instances from any stream:

Since image objects are immutable, you cannot change their properties after creation. Most image properties are set automatically using metadata in the accompanying image file or image data. The immutable nature of image objects also means that they are safe to create and use from any stream .

https://developer.apple.com/reference/uikit/uiimage

+2
source

Just to do this briefly: UIImage is not thread safe, or works better only with the main thread, as I experienced in my current thread after some debugging.

Hope this helps. I want to get more information about this from Apple, or even better, the UIImage class, which may appear in another thread. It shouldn't be too hard ...

edit: After some research, I found this to be "UIGraphicsGetImageFromCurrentImageContext ()"; what causes the problems. this is slightly different from the topic, but maybe it helps: https://coderwall.com/p/9j5dca

Thanks to Zachary Waldowski.

+1
source

Thread-safe is not a problem, since any thread can simultaneously try to access the context (at the same time). And, although this is normal, in situations with low memory, for example, with the extension for editing photos on iOS devices, two threads accessing the same context can lead to the crash of the application due to low memory.

This occurs when mixing main image filters with vImage operations. Both of them are thread safe, but ARC will not release the vImage buffer data before processing the Core Image object, so at some point you have two copies of the image in memory.

Accordingly, you never think that your knowledge of thread safety is complete without understanding thread and concurrency, and this doubles for any answer to thread safety questions. In short, the right question is how thread safety applies to memory usage whenever you talk about image processing.

If you just kick the tires here, you need to wait to ask your question until you run into a real problem. But, if you are planning your next step, you need to know how to sequentially execute image processing commands, and with manual release. You must create your application so that only one copy of the image is processed in memory. Never rely on automatic release - thread safe or not - to do this for you. THIS DOES NOT WORK.

-1
source

Source: https://habr.com/ru/post/1438227/


All Articles