Today I’m going to show you an easy way to improve the performance of table view with images. Normally, when you get images from the Internet and show it get stuck when scrolling.
We can use dispatch_async to overcome this problem. Then the ios GDC (Grand Central Dispatch) will look after that. The ios will assign the image loading to a background thread while your table will load in the main thread.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
............ | |
............ | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | |
UIImage *image = [self GetImageById:member.MemberId]; | |
dispatch_async( dispatch_get_main_queue(), ^{ | |
cell.imageView.image = image; | |
}); | |
}); | |
............... | |
............... | |
} |
Here I’m assigning the background thread to get the UIImage from my method GetImageById, by sending the member id. But to display the image in the table view we have to send that image to the main thread. That is why I’m assigning the loaded image in the dispatch_get_main_queue()block.
No comments:
Post a Comment