Мне нужно получить несколько URL-адресов фотографий с сервера и отобразить их в виде коллекции.Дело в том, что они предоставляют API под названием «findObjectsInBackgroundWithBlock», чтобы позволить мне загружать URL-адреса с сервера.Он автоматически загружается с сервера в фоновом режиме. Я пытаюсь использовать dispatch_async, но, похоже, возникли некоторые проблемы с коллекцией.Есть идеи?
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;
}