0
votes

I've got a strange thing happening I dont understand. if I run the method [self setImage:[UIImage imageNamed:@"Loader20.png"]] eg. it works fine outside the thread selector method, but it wont work in the selector method itself, it just does nothing. here is the way I do it.

- (id)init {
self = [super init];
if (self) {

    //[self setImage:[UIImage imageNamed:@"Loader20.png"]];
    [NSThread detachNewThreadSelector:@selector(showLoadingImage) toTarget:self withObject:nil];

    }
      return self;
   }


-(void)showLoadingImage{

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
int i = 0;
int imageIncrement= 1;
bool run = true;
while (run){
    [NSThread sleepForTimeInterval:1];

    if(imageIncrement == 48){
        imageIncrement=1; 
        i++;
    }else{
        imageIncrement++;
    }
    NSString *imageString = [NSString stringWithFormat:@"%@%d%@", @"Loader", imageIncrement, @".png"];
    NSLog(imageString);

    [self setImage:[UIImage imageNamed:imageString]];
    if (i == 2){
        run = false;
        [NSThread exit];
    }

}
[pool release];

 }

Can anyone give me some directions, I made sure all the images I want to loop through are correctly named testing with NSlog but still the image doesnt change.

Thanks in advance. Brett

2

2 Answers

0
votes

Most of UIKit isn't threadsafe, so you must assign the image to the imageview on the main thread (the loading however, can still be done in a background thread).

0
votes

UIKit just work in main thread. Try to load the image as a NSData in a thread, and then pass the NSData to main thread to use as UIImage.