During the image downloading from the url or from web services like XML/ JSON, and loading the same images in the image view will slow down the app or it may lead the app to get freezed, when the image downloaded in a synchronous way.
To avoid this,
- Asynchronous image downloading will avoid such circumstance, here is the coding for displaying the image from the url,
- and displaying it in the UITableView Cell,
NSString *stringURL=[NSString stringWithFormat:@"http://www.xxx.xml"]; //or it may be JSON or any php URL
NSURL *imageURL=[NSURL URLWithString: stringURL];
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//this will start the image loading in bg
dispatch_async(concurrentQueue, ^{
NSData *image = [[NSData alloc] initWithContentsOfURL:imageURL];
//this will set the image when loading is finished
dispatch_async(dispatch_get_main_queue(), ^{
cell.imageView.image = [UIImage imageWithData:image];
});
});
- Here, what we have done is, creating a separate queue (concurrentQueue) for the process of downloading the image from the URL,
- After the finish of downloading, we just displaying the images in the image view of the cell in the main queue.
No comments:
Post a Comment