Using NSThread to solve waiting for image from URL on the iPhone
- by james.ingham
So I have the following code in a method which I want to set a UIImageView image to that of one from an online source:
[NSThread detachNewThreadSelector:@selector(loadImage) toTarget:self withObject:nil];
Then in the method called by the thread I have this:
- (void) loadImage
{
NSURL *url = [NSURL URLWithString:logoPath]; // logoPath is an NSString with path details
NSData *data = [NSData dataWithContentsOfURL:url];
logoImage.image = [UIImage imageWithData:data];
}
This works great however I get many warnings within the Debugger Console along the lines of:
2010-05-10 14:30:14.052
ProjectTitle[2930:633f] *
_NSAutoreleaseNoPool(): Object 0x169d30 of class NSHTTPURLResponse
autoreleased with no pool in place -
just leaking
This occurs many times each time I call the new thread and then eventually, under no pattern, after calling a few of these threads I get the classic 'EXC_BAD_ACCESS' run-time error.
I understand that this is happening because I'm not retaining the object but how can I solve this with the code in 'loadImage' shown above?
Thanks