4

Is calling something like this considered thread safe ? It's only creating a UIImage, no UI updating. I can't find any documentation about this.

UIImage * hiResImage = [[UIImage alloc] initWithContentsOfFile:path]; 

FYI, I later do the UI update on main thread like this...

[imageViewForZoom performSelectorOnMainThread:@selector(setImage:) withObject:hiResImage waitUntilDone:NO];

What I already know:

  • Since iOS4, many drawing method of UIKit became thread safe. Read from here.
  • I should not update ui on background thread (e.g. no [myImageView setImage:image];)

EDIT: Let's look at another point of view. Does 'not-thread-safe' means that there is a chance that it can be blocked forever? or just means there is no guarantee on the start/duration execution time. If it is the latter case, then there is no problem if we would have some 'undetermined amount' of delay when loading an image. The UI update is done on main thread. So, it is still considered OK, for creating UIImage at least, to be not-thread-safe.

I know this is not really related to the question but just want to point it out since I fear there will be no clear-cut answer to my original question :)

3 Answers 3

4

My experience below is not directly with UIImage initContentsFromFile: but with UIImage imageWithData, but your question is about UIImage thread safety.

I recently had to debug an issue with using [UIImage imageWithData:] that is called from a NSURLConnetionDelegate function connectionDidFinishLoading to download images using several background threads. Since the downloaded images are used for updating the UI, I had to use [NSOperationQueue mainQueue] addOperationWithBlock ... below:

- (void) connection:(URLConnection*)connection didReceiveData:(NSData *) data {
    [imgData appendData:data];
}

- (void) connectionDidFinishLoading:(NSURLConnection*)connection {
    [NSOperationQueue mainQueue] addOperationWithBlock:^{
        UIImage *img = [UIImage imageWithData:imgData];
        // more code here to update the UI
    }];
}
  • When running on an iOS 7.x simulator, img holds a valid image
  • When running on an iOS 7.x device (iPod Touch), img is always nil

During a debugging session, I noticed that the issue (temporarily) disappeared when the debugger stepped through each statement one line at a time. My theory is that running in debugger step-mode does not put UIImage to handle concurrent threads running imageWithData. Therefore, I believe that UIImage imageWithData (and perhaps other similar functions) are not thread-safe.

Using a @synchronized block seems to solve the issue

- (void) connectionDidFinishLoading:(NSURLConnection*)connection {
    [NSOperationQueue mainQueue] addOperationWithBlock:^{
        @synchronized(imgData) {
            // Run the following in a synchronized block
            UIImage *img = [UIImage imageWithData:imgData];
        }
        // more code here ....
    }];
}
2

According to Apple, the answer is yes, it is safe to create a UIImage from any thread:

Because 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 thread.

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

1

Yes. It is fairly common practice to load images in the background, mainly if its a remote file, or if many images are being loaded. And yes, only update UI on the main thread.

EDIT:

Due to some enlightening comments, I would revise my first answer of 'Yes' to 'Based on experience and my assessment of what wouldn't be a viable alternative for UIImage's thread safety when it comes to loading an image, I think it's reasonable to assume it is. However, each person is warranted their own opinion, and perhaps his or her risk associated with code failure here is too high to make assumptions under any circumstances.'

11
  • 1
    @Matt Q: Is creating UIImage thread safe? A: Yes… -- do you have a reference for that?
    – justin
    Oct 7, 2012 at 6:27
  • There is documentation in a comment from this duplicate question: stackoverflow.com/questions/10645307/thread-safety-of-uiimage
    – Matt
    Oct 7, 2012 at 18:56
  • 1
    @Matt the docs you have linked pertain to drawing UIImages. however, the referenced docs say nothing about creation. as far as i am concerned, jfortmann and CodaFi are the contributors to the linked QA one should pay attention to. there's a lot behind the scenes with images -- that stuff in addition to the object is not necessarily thread safe until the guarantee is published. sure, people do it, but that doesn't mean it's supported -- an OS update or even differences in multithreading workloads (device or workload variance) have the potential to break the program that does this. (cont)
    – justin
    Oct 8, 2012 at 1:59
  • (cont) it's actually a really good question, and i think proper support is a long time coming. you might think i am paranoid, but i have been using other approaches to load images on secondary threads the whole time because (to my knowledge) that guarantee never existed.
    – justin
    Oct 8, 2012 at 2:02
  • 1
    @Eiko the default is that UIKit (and AppKit) types should be used from the main thread unless specified differently.
    – justin
    Oct 11, 2012 at 5:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.