【问题标题】:How to get image in collectionview cell from json using objective c如何使用objective c从json获取collectionview单元格中的图像
【发布时间】:2019-04-22 20:46:04
【问题描述】:
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单元格中的图像?

【问题讨论】:

  • 请以小写字母开头命名您的变量。不要将多个数组用于同步的值。而是将字典解析为自定义类。还有strimage是什么?那是网址吗?那是 Base64Image 数据吗?

标签: objective-c iphone uicollectionview uiimageview


【解决方案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;
    });
});

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

【讨论】:

  • 它在数据中返回 nil 吗?
  • 没有已知的选择器'dataFromBase64String:'的类方法
  • 感谢兄弟图片仍未显示在收藏视图中
  • 你有数据还是没有?检查一下
  • [ { 类型:“0”,图片:“metro-online.pk/dashboard/images/1617749124.png”,catcode:“221”,级别:“2”,brandcode:“0”,brandcampaignpage:“0”,促销代码: "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" }, ]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
相关资源
最近更新 更多