Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread safety of UIImage

I know that Apple officially recommends UIKit to be used in the main thread only. However, I've also heard claims that UIImage is thread-safe since iOS 4.0. I cannot find any documentation backing this claim.

Does anyone have any information to support this claim? As a class used to store data and decode image data, UIImage should be thread-safe if well designed.

like image 743
Evil Nodoer Avatar asked May 18 '12 00:05

Evil Nodoer


2 Answers

Directly from Apple's documentation for UIImage

Image objects are immutable, so you cannot change their properties after creation. This means that you generally specify an image’s properties at initialization time or rely on the image’s metadata to provide the property value. It also means that image objects are themselves safe to use from any thread. The way you change the properties of an existing image object is to use one of the available convenience methods to create a copy of the image but with the custom value you want.

(Emphasis mine)

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

like image 67
Eric G Avatar answered Sep 28 '22 12:09

Eric G


It is true that apple recommends using elements from the UIKIt on the main thread:

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.

Since UIImage isn't derived from UIResponder, and you do not actually display it on the interface/screen. Then doing operations with UIImages on another thread should be safe.

This is however based on my experience, I haven't seen any official documentation about it.

like image 20
Pochi Avatar answered Sep 28 '22 11:09

Pochi