如何使用目标 c 从 json 获取 collectionview 单元格中的图像

How to get image in collectionview cell from json using objective c

提问人:Rehan Meo 提问时间:11/20/2018 最后编辑:Rahul AgarwalRehan Meo 更新时间:11/20/2018 访问量:36

问:

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;
}

如何使用目标 c 从 json 获取 collectionview 单元格中的图像?

Objective-C iPhone UIClecutionView UIImageView

评论

0赞 Larme 11/20/2018
请以小写字母开头命名变量。不要对同步的值使用多个数组。而是将字典解析为自定义类。还有什么是?那是 URL 吗?这是 Base64Image 数据吗?strimage

答:

0赞 Andrey Chernukha 11/20/2018 #1

这是你应该如何做到的:

 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;
    });
});

另外:它与问题无关,但不要给变量大写名称

评论

0赞 Andrey Chernukha 11/20/2018
它是否在数据中返回 NIL?
0赞 Rehan Meo 11/20/2018
选择器“dataFromBase64String:”没有已知的类方法
0赞 Rehan Meo 11/20/2018
谢谢兄弟图像仍未显示在集合视图上
0赞 Andrey Chernukha 11/20/2018
你有数据还是没有数据?检查一下
0赞 Rehan Meo 11/20/2018
[ { type: “0”, image: “metro-online.pk/dashboard/images/1617749124.png”, catcode: “221”, level: “2”, brandcode: “0”, brandcampaignpage: “0”, promocode: “0”, weburl: “0”, webtext: “0”, productcode: “0”, text: “空调”, browserurl: “0” }, { type: “34”, image: “metro-online.pk/dashboard/images/1507248489.png”, catcode: “0”, level: “0”, brandcode: “0”, brandcampaignpage: “0”, promocode: “0”, weburl: “0”, webtext: “0”, productcode: “0”, text: “家庭娱乐”, browserurl: “0” }, ]