如何从JSON响应中加载图像?

时间:2015-09-17 07:35:51

标签: ios arrays json image

我使用以下代码从json响应加载图像并在UICollectionView中显示图像。但是节计数中的numberofItems确实返回0。这意味着imageArray2.count返回0.它不起作用。有人能告诉我在下面的代码中我做错了吗? imageArray2NSArray

// JSON RESPONSE

claimImages =     (
                {
            "image_url" = "http://zapponomics.net/claimservice/parcelimage/555506520image0.jpg";
            "img_for" = "Front view of parcel";
            "pro_number" = Rita;
        }
    );
    images =     (
                {
            "image_url" = "http://zapponomics.net/claimservice/parcelimage/384270647image0.jpg";
            "img_for" = "Front view of parcel";
            "pro_number" = Rita;
        }
    );
 NSMutableDictionary *imageDict2 = [jsonDict valueForKey:@"claimImages"];
                NSLog(@"Image Dictionary :- %@",imageDict2);
                imageArray2 = [imageDict2 valueForKey:@"image_url"];
                NSLog(@"My Array Image :- %@",imageArray2);

    -(void)loadImageFromURL2:(NSURL *) url callback:(void(^)(UIImage *image1))callback
    {
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
        dispatch_async(queue, ^{
            NSData *imageData1 = [NSData dataWithContentsOfURL:url];
            dispatch_async(dispatch_get_main_queue(),^{
                UIImage *image1 = [UIImage imageWithData:imageData1];
                callback(image1);
            });
        });
    }
     if (collectionView.tag == 601)
        {
            cell = [afterParcelCollectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
            UIImageView *myImageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 50, 80)];
            myImageView2.tag = 102;
            [cell.contentView addSubview:myImageView2];

            NSString *myImage1= [imageArray2 objectAtIndex:indexPath.row];
            [self loadImageFromURL2:[NSURL URLWithString:[myImage1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding ]] callback:^(UIImage *image1){

                myImageView2.image =image1;
            }];
        }

              return cell;

1 个答案:

答案 0 :(得分:0)

尝试以下代码并替换您的代码:

NSArray *claimImagesArray = [jsonDict valueForKey:@"claimImages"];
NSLog(@"%@", claimImagesArray);


NSString *imagePath = [[claimImagesArray objectAtIndex:0] valueForKey:@"image_url"];
NSLog(@"My Image :- %@", imagePath);