Reputation: 33
I have an array of objects. My object contains a URL(NSString
) and an image (UIImage
). I call a webservice to populate all the urls for the images and then I want to asynchronously get the image and update the object with the image.
I am stuck while I am trying to determine the index of the array which I need to update when the data is received from URLConnection. Any thoughts?
Upvotes: 1
Views: 756
Reputation: 2676
I would suggest you to use GCD to download image in background. Here is the tutorial if you want to refer:- http://blog.slaunchaman.com/2011/02/28/cocoa-touch-tutorial-using-grand-central-dispatch-for-asynchronous-table-view-cells/
Following is the Code you can use:-
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
dispatch_sync(dispatch_get_main_queue(), ^{
[yourImageView setImage:image];
});
});
Upvotes: 0
Reputation: 5026
You need to map the NSURLConnection to your Array. You could either subclass NSURLConnection to add a member, wich allows storing a tag identifier, such as an index.
Probably it would be simpler to create a NSDictionary, where each NSURLConnection acts as key and the arrayIndex in an NSNumber would be stored as the value.
In the callback-method you are using, you could access your dictionary with the according NSURLConnection as its key.
int arrayIndex = [[myDictionary objectForKey:urlConnection] intValue]
Upvotes: 1
Reputation: 6152
For getting UIImage from net use this instead of URLConnection multiple times.
UIImage *image=[[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL of the image"]]];
Then load your image asynchronously in the UIImageView.
Upvotes: 0