Objective c 显示从json(图像url)vlaue到CollectionView的图像
您好,我尝试将图像从json显示到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
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;