I have problems to obtain image from url on iPad 3 4G test device.
Code works with ipad 1 & 2 and also with iphone 3gs and iphone 4.
But seems that image is nil when requested from server.
I don’t experience the issue, but clients report problems…
I belive that problem is in the following code
Code:
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.image.com/img_download/499441.jpg"]];
UIImage *thumbnail = [UIImage imageWithData:data];
Url in this code is not real but it is correct for clarification.
Thanks!
A couple of thoughts:
I’d suggest using
dataWithContentsOfURL:options:error:instead of justdataWithContentsOfURLand reporting the error code is. Or useNSURLConnectionor other framework (e.g.AFNetworking?) that might provide greater diagnostic information. But I suspect thatdataWithContentsOfURL:options:error:can provide the necessary error information.Can you place this in context? For example, is this something that your app is doing asynchronously (e.g. thumbnails for a tableview)? If so, are you using
NSOperationQueuewith a reasonably smallmaxConcurrentOperationCountrather than a global dispatch queue, to make sure you don’t exceed the number of permissible concurrent connections?For example, if you’re updating your tableview like so:
you should do the following. First, define a class property:
Second, you should initialize this in
viewDidLoad, specifying how many concurrent operations are acceptable:And finally, replace
dispatch_asyncwith:Note, you may also want to do caching to eliminate reloading images that you’ve already downloaded. See https://stackoverflow.com/a/14444605/1271826 for an example. You could also cache images to
Documentsfolder if you want to cache images across sessions (though some care must be done to determine changes in images).To my second point, sometimes connection problems are not caused by the particular line that fails, but is a symptom of a broader problem, and as such, we need a little more info about how you’re using the code in your question.