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?
The problem is that you’re giving
dataWithContentsOfURLa remote URL. It takes to time to download it, plus it might never arrive. That’s risky. Instead, show a placeholder and useNSURLConnectionto 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.