Skip to content

Instantly share code, notes, and snippets.

@thornpig
Last active January 26, 2016 04:34
Show Gist options
  • Save thornpig/8daeba3a42935718d8e7 to your computer and use it in GitHub Desktop.
Save thornpig/8daeba3a42935718d8e7 to your computer and use it in GitHub Desktop.
Make sure reused cell presents the right picture with GCD
id imageSource = cell.imageSource;
dispatch_async(queue, ^{
//block captures imageSource
UIImage *thumbNail = getImageFromSource(imageSource);
dispatch_sync(dispatch_get_main_queue(), ^{
//block captures cell
//if imageSource does not match cell.imageSource, it means the cell is resued before the background thread returns the image,
// and the image requested for the already invisible cell may be returned after the current cell obtains its image, in which case
// the returned image should not be used.
if ([imagaeSource isEqualToArray: cell.imageSource])
{
cell.imageView.image = thumbNail;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment