views:

119

answers:

2

Hey all, here's the deal...

I've got a UIImage that is being loaded in the background via an NSURLConnection within a subclassed UIImageView. Until the data finishes downloading, a placeholder image is shown. The UIImage needs to be used by another, separate UIImageView, as well.

The problem I'm having is that if I set the second UIImageView's image property to that of the subclassed object before the download is complete, the second UIImageView never displays the downloaded image, since it's image prop is pointing to the placeholder.

Is there anyway to pass a pointer to a pointer between these two UIImageViews?

I've tried things like this:

imageView2.image = &[imageView1 image];
imageView2.image = *[imageView1 image];

But these don't seem to work. Any ideas?

+2  A: 

Those pointers look particularly gross. The problem with doing it that way is that, even though you might be updating the data pointed to by those pointers, you are not notifying your UIImageView subclasses that the data has changed, and thus they don't know to redraw.

Instead of playing with that pointer mess, why not use Key-Value Observing instead? If you have a separate thread running that downloads the UIImage, you can just tell your UIImageViews to observe that property and when your downloader class is finished downloading all the data and has put it into the property, the views will get a notification and can then display the image.

Marc W
You can also use NSNotificationCenter to post a notification when the data has finished downloading. This keeps you from having to set a delegate or keep any other kind of reference to the observer.
Matt Long
One added piece to the puzzle... The two UIImageViews are completely unaware of each other. The UIViewController which creates the downloader subclass also creates another view controller which contains the second UIViewController. Also this is done several times through a for loop, and an array of images is passed. I tried to simplify the case for the example. Anyway. I'm going to look into notifications and observing.
Brad
It's okay if the views are unaware of each other. They both are looking for notifications (KVO or otherwise) from the downloader class, not from each other. They will remain uncoupled with this solution.
Marc W
Thanks Marc W. and Matt L. I was (at first) unable to get either of your solutions working, and was about to come back here and call foul, until I remembered that `foo.image = bar` is not the same as `[foo setImage:bar]`.Anyway, my observers are set and all is right with the world. Thanks.
Brad
A: 

Can't you just catch the connectionDidFinishLoading: message from the NSURLConnection and then set the second image?

Epsilon Prime