tacos_tacos_tacos
tacos_tacos_tacos

Reputation: 10585

UIImage imageWithData takes a few moments to appear

I have a typical "drill down" style app with a DetailViewController that includes a UIImageView, UIWebView, and a few UILabels. The problem is that imageView takes a while longer to load! It is white (the background color) for a few moments, then finally loads after 3-5 seconds (probably the amount of time it takes to download).

The code is:

- (void)loadSummary {
    self.dogFood = [[IKFetcher sharedFetcher] fetchDogFoodInfoForId:self.dogFoodId];
    self.dogFoodAnalysisArray = [[IKFetcher sharedFetcher] fetchDogFoodAnalysisArrayForId:self.dogFoodId];
    self.dogFoodRating = [[IKFetcher sharedFetcher] getAvgDogFoodRatingForId:self.dogFoodId];
    if ([self.dogFood.dfImageUrl length] != 0) {
        self.dogFoodImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.dogFood.dfImageUrl]]];
    }
}

and the call to loadSummary is wrapped in a dispatch_async call.

What can I do to make the app wait for the image to download?

Upvotes: 1

Views: 611

Answers (2)

Dejell
Dejell

Reputation: 14337

I would suggest

SDWebImage so you don't need to implement it yourself.

Upvotes: 0

matt
matt

Reputation: 536027

The problem is that you're giving dataWithContentsOfURL a remote URL. It takes to time to download it, plus it might never arrive. That's risky. Instead, show a placeholder and use NSURLConnection to download the real image in the background; it will call the delegate when it's got the image and now you can display it instantly.

Upvotes: 8

Related Questions