常常有機會在CollectionView或是TableView中撈取大量圖片,
如果使用同步的方式會造成畫面延遲不順,
這時就要使用非同步的方式來撈取圖片。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSString *imgName = imageName; NSString *imgPath = [NSString stringWithFormat:@"http:xxxx/%@",imgName]; NSURL *imgURL = [NSURL URLWithString:imgPath]; NSData *imgData = [NSData dataWithContentsOfURL:imgURL]; UIImage *image = [UIImage imageWithData:imgData]; dispatch_async(dispatch_get_main_queue(), ^{ cell.image = image; }); });