So this code basically gets data from a server, creates an image, and then adds it to the view.
However, I would like it to update the view one at a time.
Right now, the view is updated with all the images at once after all are loaded.
I would like to update the view, everytime a new image is uploaded - This should happen in the addImage method, but instead, it is just waiting for all the images to load.
I tried using a dispatch_async to solve this problem, but it did not work.
So how do I load images one at at a time?
AFJSONRequestOperation *operation =
[AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
//some code not displayed here that parses the JSON
for (NSDictionary *image in images){
if ([[image objectForKey:@"ContentType"] isEqualToString:@"image/png"]){
NSString *imgLink = [[image objectForKey:@"MediaUrl"] JSONStringWithOptions:JKSerializeOptionPretty includeQuotes:NO error:nil];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data cache:NO]
dispatch_async(dispatch_get_main_queue(), ^{
[self addImage:img];
//This method adds the image to the view
});
}
}
}
Update: This is not a tableview, images are added as subviews to a UIView
I believe you are looking for a form of lazy loading. Try this example: Apple Lazy Loading example
is a synch operation so you have to wait for each image one by one, a proper lazy loading implementation of the picture based on the url is the best solution, in the meantime you can try to do something like this
if you want to use this solution a recommend you to create your own concurrent queue rather than use the global_queue