如何使用 objective c 从 json 获取 collectionview 单元格中的图像
How to get image in collectionview cell from json using objective c
self->SCImageArr = [[NSMutableArray alloc]init];
self->SCLabelArr = [[NSMutableArray alloc]init];
NSDictionary *SCDic = [maindic objectForKey:@"specialcategories"];
for (NSDictionary *SCDictionary in SCDic){
NSString *strimage = [SCDictionary objectForKey:@"image"];
[self->SCImageArr addObject:strimage];
NSLog(@"SCImage :%@", strimage);
NSString *strname = [SCDictionary objectForKey:@"text"];
[self->SCLabelArr addObject:strname];
NSLog(@"SClabelname :%@", strname);
}
[self.specialCategoriesCollectionView reloadData];
我的手机密码在这里
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIndentifer = @"Cell";
SpecialCategoriesCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIndentifer forIndexPath:indexPath];
cell.categoriesImages.image = [UIImage imageWithData:SCImageArr];
cell.categoriesNameLabel.text = self.specialCategoriesLabel[indexPath.row];
return cell;
}
如何使用 objective c 从 json 获取 collectionview 单元格中的图像?
您应该这样做:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSURL* url = [NSURL URLWithString:SCImageArr[indexPath.row]];
NSData* data = [NSData dataWithContentsOfURL:url];
UIImage* image = [UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
cell.categoriesImages.image = image;
});
});
另:与题目无关,但变量名不要大写
self->SCImageArr = [[NSMutableArray alloc]init];
self->SCLabelArr = [[NSMutableArray alloc]init];
NSDictionary *SCDic = [maindic objectForKey:@"specialcategories"];
for (NSDictionary *SCDictionary in SCDic){
NSString *strimage = [SCDictionary objectForKey:@"image"];
[self->SCImageArr addObject:strimage];
NSLog(@"SCImage :%@", strimage);
NSString *strname = [SCDictionary objectForKey:@"text"];
[self->SCLabelArr addObject:strname];
NSLog(@"SClabelname :%@", strname);
}
[self.specialCategoriesCollectionView reloadData];
我的手机密码在这里
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIndentifer = @"Cell";
SpecialCategoriesCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIndentifer forIndexPath:indexPath];
cell.categoriesImages.image = [UIImage imageWithData:SCImageArr];
cell.categoriesNameLabel.text = self.specialCategoriesLabel[indexPath.row];
return cell;
}
如何使用 objective c 从 json 获取 collectionview 单元格中的图像?
您应该这样做:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSURL* url = [NSURL URLWithString:SCImageArr[indexPath.row]];
NSData* data = [NSData dataWithContentsOfURL:url];
UIImage* image = [UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
cell.categoriesImages.image = image;
});
});
另:与题目无关,但变量名不要大写