首页 > ios 关于循环异步的问题

ios 关于循环异步的问题

for (int i = 0; i < array.count; i++) {

[_stagePhotoView sd_setImageWithURL:[[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@?imageView/w/4",array[i]]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        [_imageArray addObject:image];
    }];

}

这是一个循环里面以一个异步去取缓存图片然后存到一个数组内存中 但是这会有一个问题 以为是异步 所以有时候图片没拿到 循环就直接往下走 有什么办法 让循环一次必须在异步取完图片之后再循环下一次 真心求解答 或者我的做法本身就有问题 能给一个更好的思路吗 在线等


_imageArray 可以先放入array.count个image,block里面根据imageView的tag来判断获取的第几张图片,然后block里面用数组根据index的方法替换image.

[arr removeObjectAtIndex:imageView.tag];
[arr insertObject:image atIndex:imageView.tag];

也就是说整个过程是一个在子线程的阻塞队列吧。
这种情况不要用sd_setImageWithURL这个方法了,因为你图片获取来了是令做他用。这个时候你可以用
SDWebImagePrefetcher.h里面的


/**
 * Assign list of URLs to let SDWebImagePrefetcher to queue the prefetching,
 * currently one image is downloaded at a time,
 * and skips images for failed downloads and proceed to the next image in the list
 *
 * @param urls list of URLs to prefetch
 */
- (void)prefetchURLs:(NSArray *)urls;

如果要一个一个下载的话,可以把并行数maxConcurrentDownloads设置为1
可以通过实现SDWebImagePrefetcherDelegate代理方法来检测每一张图片是否下载完成。
具体要拿到某一张图片,可以通过url从缓存里直接取


为什么非得在这样子把image装进数组,把ImageView装进数组也行啊,给imageView加tag,通过tag判断是第几张图片
【热门文章】
【热门文章】