Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 显示从json(图像url)vlaue到CollectionView的图像_Objective C_Json_Uicollectionview - Fatal编程技术网

Objective c 显示从json(图像url)vlaue到CollectionView的图像

Objective c 显示从json(图像url)vlaue到CollectionView的图像,objective-c,json,uicollectionview,Objective C,Json,Uicollectionview,您好,我尝试将图像从json显示到CollectionView 我的代码是 NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://zoo.com/jh/image.php"]]; [request setHTTPMethod:@"GET"]; [request setValue:@"application/json;charset=UTF-8" for

您好,我尝试将图像从json显示到CollectionView

我的代码是

   NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://zoo.com/jh/image.php"]];

[request setHTTPMethod:@"GET"];

[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];

NSError *err;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];


NSArray *headlines=[jsonArray1 objectForKey:@"image_gallery"];

for (int i=0;i<[headlines count]; i++)
{
    NSString *moblinks=[[headlines objectAtIndex:i]objectForKey:@"image_url"];

    [self.itemshowdetailsAr objectAtIndex:moblinks];
    NSLog(@"%@",moblinks);

}
我的收藏视图代码

      - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView    cellForItemAtIndexPath:(NSIndexPath *)indexPath
      {
       CollectionViewCell *cell = [collectionView  dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

    NSString *ImageURL =[self.itemshowdetailsAr objectAtIndex:indexPath.row];

    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];

    cell.imageView.image = [UIImage imageWithData:imageData];


            cell.imageView.userInteractionEnabled=YES;

            cell.imageView.contentMode = UIViewContentModeScaleToFill;
           // cell.imageView.tag=i-1;



           return cell;
         }

但我只得到了空的集合视图。我哪里出错了?我在控制台中得到了url数组,但cell没有显示图像。

从外观上看,您试图在主线程上下载图像,我猜在处理网络请求时,UI可能会冻结。相反,通过执行以下操作,异步加载图像并远离主线程

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView    cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CollectionViewCell *cell = [collectionView  dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
NSURLSession *session=[NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[self.itemshowdetailsAr objectAtIndex:indexPath.row]]] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        cell.imageView.image = [UIImage imageWithData:data];
        cell.imageView.userInteractionEnabled=YES;
        cell.imageView.contentMode = UIViewContentModeScaleToFill;
    }];
}] resume];
return cell;
}

NSURLSession任务将从主线程运行。下载imageData后将调用完成处理程序。此时,将向主线程添加一个操作,以更新collectionViewCell中的imageView

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView    cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CollectionViewCell *cell = [collectionView  dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
NSURLSession *session=[NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[self.itemshowdetailsAr objectAtIndex:indexPath.row]]] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        cell.imageView.image = [UIImage imageWithData:data];
        cell.imageView.userInteractionEnabled=YES;
        cell.imageView.contentMode = UIViewContentModeScaleToFill;
    }];
}] resume];
return cell;