-1

I am working on an app that does image processing and displays the resulting image. Im using UIScrollView to let user scroll all images, because the image is not a standard jpg or png, it takes time to load. so i want to use a thread to load image, and then update the views.

For now, I use a timer in the background thread to check whether there are any images that are needed to be loaded. but it is not working so well. I want to know whether there is a way to notify the background thread when there are some images that are needed to be loaded in the main thread or any other suggestions?

Thanks in advance.

chancyWu
  • 14,073
  • 11
  • 62
  • 81
  • if you load something from the net the class which you use normally has something like a callback block or a delegate. The block/delegate will be called when the download finishes – Pfitz Sep 12 '12 at 06:53
  • @Pfitz Yes,some of images are downloaded from Internet.But i download all images first. and then when user scrolling, i load some of images to display depending on its index. – chancyWu Sep 12 '12 at 06:57

1 Answers1

1

Provide a method in your class that controls the scrollView, lets call in 'processImage'. In your background thread, when you have an image, send it to the UI class as follows:

dispatch_async(dispatch_get_main_queue(), ^{ [uiClass processImage:theImage] } );

The background object should keep a weak reference to the uiClass (which is a delegate in this example). The idea is to do the image processing in the background, but provide it to the UI class on the main thread.

David H
  • 40,852
  • 12
  • 92
  • 138
  • thanks for your suggestion. GCD is a great mechanism. i am trying use this mechanism to do the work. performance is better than before. – chancyWu Sep 13 '12 at 04:32