iWheelBuy
iWheelBuy

Reputation: 5679

Doesn't load picture from valid URL

UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]]];

I use the code above and then check

if (image)
    return image;
else
{
    NSLog(@"no image on URL");
    return nil;
}

But sometimes (very very seldom) I don't get an image from a valid url. The url is valid 100%.

Usually it takes nearly one second to load a picture, but when it can't load a picture the process takes much more time (20-200 seconds). And then i get "no image on URL".

Is there a better way to get a picture from URL? I'd rather get "no image on URL" in one second then waiting so long.

P.S. srry for my poor english

Upvotes: 0

Views: 182

Answers (1)

Rob Napier
Rob Napier

Reputation: 299355

It is very rare that you want to use dataWithContentsOfURL:. It's a blocking call so requires a background thread. It's also inflexible and doesn't provide good error returns (which is the problem you're encountering).

See the URL Loading System Programming Guide. Generally you'll want to configure an asynchronous NSURLConnection for this kind of work. If you're doing a lot of network operations, you may want to consider a framework like MKNetworkKit or AFNetworking which handle a lot of the complexities for you.

Upvotes: 1

Related Questions