gpt4 book ai didi

ios - 从 URL 和缓存中一张一张地加载和显示图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:10:48 25 4
gpt4 key购买 nike

我正在使用 SDWebImage 库在带有页面控件的 ScrollView 中显示图像。

如何一张一张地下载图片并显示出来。假设我有 4 个 URL:url1、url2、url3、url4所以我想先显示 url1 然后...2,3,4..等等...2,3,4 应该在后台加载,直到没有下载占位符图像显示在那里。

下面是我的for循环内部代码,我试过它完美吗?

        NSString* serverImgicon=<url here in a loop>;
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
NSURL *url=[NSURL URLWithString: serverImgicon];
//set your image on main thread.
dispatch_async(dispatch_get_main_queue(),^{
[img sd_setImageWithURL:url
placeholderImage:[UIImage imageNamed:@"placeholderImg"]];
});
});

最佳答案

试试下面的代码,它会工作正常

 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

CollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

myobject *newobject = _myarray[indexPath.item];
cell.nameLabel.text =newobject.name;

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{

[cell.activityIndicatorForImage startAnimating];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString:newobject.image]]];
dispatch_sync(dispatch_get_main_queue(), ^{
if(image!= nil){
[[cell myimage] setImage:image];
[cell.activityIndicatorForImage setHidden:NO];

}else{
UIImage * image = [UIImage imageNamed:@"PLACEHOLDER"];
[[cell myimage] setImage:image];
[cell.activityIndicatorForImage setHidden:YES];
[cell.activityIndicatorForImage stopAnimating];

}


[cell setNeedsLayout];
});
});

return cell;
}

关于ios - 从 URL 和缓存中一张一张地加载和显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43840475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com