views:

35

answers:

0

When an image is loading, I want to hide the old image and display an activity indicator. The following code should (IMO) work, however, the image does not hide and no activity indicator is displayed. If the image has not been downloaded previously, there is a discernable wait whilst it does download. What am I doing wrong?

- (UIImage *)loadImage:(int)imageNumber {
BOOL imageLoaded = NO;

self.weatherImageView.hidden = YES;
[self.activityIndicator startAnimating];

while (!imageLoaded) {
// check to see if the image has already been downloaded.
// if not, get it and add it to an array
if ([imageData count] < totalNumberOfImages && (imageNumber + 1) > [imageData count]) {
    NSString *urlString = [[mainMenuItem.imageDetails objectAtIndex: imageNumber] objectForKey: @"url"];
    NSURL *url = [NSURL URLWithString: urlString];
    NSData *data = [NSData dataWithContentsOfURL: url];
    UIImage *image = [[UIImage alloc] initWithData: data];
    [imageData insertObject: image atIndex: imageNumber];
    [image release];
}
    imageLoaded = YES;
}

[self.activityIndicator stopAnimating];
self.weatherImageView.hidden = NO;

currentImageNumber = imageNumber;

// return the image from the array
return [imageData objectAtIndex: imageNumber];

}

Additionally, the button that is pressed to load a new image stays in its highlighted state until the new image is loaded. Why is this?


GDB output for crash:

2010-09-13 21:24:56.705 PilotsMate[41643:5f03] * attempt to pop an unknown autorelease pool (0x6025a00)

2010-09-13 21:24:56.710 PilotsMate[41643:5f03] bool _WebTryThreadLock(bool), 0x5b39180: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...