objective-c - ios 关于循环异步的问题
ringa_lee
ringa_lee 2017-04-17 17:27:18
[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];
    }];

}

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

ringa_lee
ringa_lee

ringa_lee

全部回复(3)
高洛峰

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

[arr removeObjectAtIndex:imageView.tag];
[arr insertObject:image atIndex:imageView.tag];
PHPz
为什么非得在这样子把image装进数组,把ImageView装进数组也行啊,给imageView加tag,通过tag判断是第几张图片
ringa_lee

也就是说整个过程是一个在子线程的阻塞队列吧。
这种情况不要用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从缓存里直接取

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号