sharon
sharon

Reputation: 580

Error while displaying an Image

I get the following error, and i have no clue how to avoid it, can someone please help me

bool _WebTryThreadLock(bool), 0x1ea990: 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...

 - (void)loadImg {
        NSData* image = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://images.sugarscape.com/userfiles/image/DECEMBER2010/Lozza/HarryStyles2.jpg"]];
        UIImage* img = [[UIImage alloc] initWithData:image] ;
        [self performSelectorInBackground:@selector(showImg:) withObject:img];
    }

Upvotes: 0

Views: 138

Answers (3)

Chen-Hai Teng
Chen-Hai Teng

Reputation: 753

According to Threading Programmin Guide: Thread Safety Summary, you should invoke UIView's operations on main thread. Try [self performSelectorOnMainThread:@selector(showImg:) withObject:img waitUntilDone:YES].

Upvotes: 0

Paul.s
Paul.s

Reputation: 38728

It is not safe to update UI from a background thread, which I am assuming you are doing here

[self performSelectorInBackground:@selector(showImg:) withObject:img];

I think you may have got it the wrong way round you want to:

  1. Retrieve the image on a background thread
  2. Update the UI with this image on the main thread

Upvotes: 0

Costique
Costique

Reputation: 23722

Don't call any UIKit methods from threads other than the main thread. performSelectorInBackground:withObject: executes showImg: on a secondary thread, and if you call any UIKit methods there, that might be the problem.

It is actually explained in plain English:

This may be a result of calling to UIKit from a secondary thread

Upvotes: 3

Related Questions