2014-06-20 57 views
0

嗨我试图显示图像从json到CollectionView。从json显示图像(url的图像)vlaue收集查看

我的代码是...

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

} 

和我的结果在控制台

 2014-06-20 15:05:27.005 Collection[3945:60b] http://xxxxxx/jh/ju_image/gal_1.jpg 
    2014-06-20 15:05:27.007 Collection[3945:60b] http://xxxxxx/jh/ju_image/gal_2.jpg 
    2014-06-20 15:05:27.007 Collection[3945:60b] http://xxxxxx/jh/ju_image/gal_3.jpg 
    2014-06-20 15:05:27.007 Collection[3945:60b] http://xxxxxx/jh/ju_image/gal_4.jpg 
    2014-06-20 15:05:27.008 Collection[3945:60b] http://xxxxxx/jh/ju_image/gal_5.jpg 
    2014-06-20 15:05:27.008 Collection[3945:60b] http://xxxxxx/jh/ju_image/gal_6.jpg 

我的CollectionView代码

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

,但我得到空的集合视图only.where我做了错误?我在控制台中获得了一系列url,但cell没有显示图像。

回答

0

看起来你试图在主线程下载图像,我猜测用户界面可能会在网络请求处理时冻结。相反,从主线程通过执行类似

- (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。