ios - 我正在尝试从 collectionview 中显示的 URL 下载照片列表,但我无法找到执行此操作的方法
问题描述
我需要从服务器获取照片的几个 URL 并将它们显示在 collectionview 中。问题是,他们提供了一个名为“findObjectsInBackgroundWithBlock”的 api,让我从服务器下载 URL。它会在后台自动从服务器下载。我正在尝试使用dispatch_async,但collectionview似乎有一些问题。有任何想法吗?
BmobQuery *picturesB = [BmobQuery queryWithClassName:@"AssetPicture"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[picturesB findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error)
{
for (BmobObject *obj in array)
{
[photourl addObject:[obj objectForKey:@"imageUrl"]];
}
for (int i = 0; i < 12; i++) {
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:photourl[i]]];
[self.arr addObject:[UIImage imageWithData:imageData]];
}
}];//Bmob query
if(self.arr!=nil)
{
dispatch_async(dispatch_get_main_queue(), ^{
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
self.collectionView = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:flowLayout];
[self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"MyCollectionCell"];
self.collectionView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.collectionView];
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
[self.collectionView registerClass:[CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];
});// dispatch
}//if
});//dispatch
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCollectionCell" forIndexPath:indexPath];
cell.imageView.image = [self.arr objectAtIndex:indexPath.row];
NSLog(@"photo done");
cell.descLabel.text = [[NSString alloc] initWithFormat:@"{%ld,%ld}",indexPath.section,indexPath.row];
return cell;
}
解决方案
推荐阅读
- java - RxJava:onErrorReturn 在返回 Set 时出现强制转换问题
- functional-programming - 为什么函数接收额外的参数
- javascript - 在 CSS 滚动快照中调整浏览器大小的问题
- excel - 从 Linux 服务器将 csv 转换为 xlsb
- android - 屏幕关闭时我无法播放任何声音
- assembly - 修复“NASM 错误:操作码和操作数的无效组合”以在实模式上添加
- swift - SwiftUI 和 MVVM - 模型和视图模型之间的通信
- regex - 谷歌分析目标中的正则表达式帮助
- c# - 内存分配时间(快速方式)
- javascript - Angular 8:在组件中调用外部 JavaScript 函数