wrjohns
wrjohns

Reputation: 494

continuously update an NSImageView

I have an NSImageView that needs to redraw a constantly updating image. I have a model object that creates the NSImage, and a controller that links this image to the NSImageView.

With multithreading, I can easily have the model object continuously update its output image in a background thread. But as UI interactions are supposed to be done on the main thread, I can't set the NSImageView's image on a background thread. If I update the NSImageView on the main thread, the program would become unresponsive

Upvotes: 1

Views: 932

Answers (2)

Parag Bafna
Parag Bafna

Reputation: 22930

Do not update NSimageView on background thread. it is not thread safe. you should use performSelectorOnMainThread:withObject:waitUntilDone:

Upvotes: 0

Tommy
Tommy

Reputation: 100652

I hate to be contradictory but why do you think that updating the image on the main thread would make the program unresponsive?

If calculating the next NSImage is instantaneous then logically you should just calculate and display the last one since none of those in between will ever be perceived.

If calculating the next one costs some time then do the calculation off the main thread then push the image to the view on the main thread (eg, using performSelectorOnMainThread:...). The main thread won't become unresponsive because it has all that time while you're calculating the next image to deal with other events.

If updates are more frequent than the display's frame rate then you can probably go one better by setting yourself up with a CVDisplayLink and polling whatever is updating the image to get the latest one upon every tick of that.

Upvotes: 1

Related Questions