【发布时间】:2015-08-26 04:35:36
【问题描述】:
我是 iOS 新手,我从 json 获取图像 url。现在我想在集合视图上显示它。我正在做下面的代码。但无法在集合视图中显示图像。有人可以帮我解决这个问题吗?enter code here
// 集合查看代码
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return imageArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
if(!cell)
{
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
UIImageView *myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(50,0, 50, 80)];
myImageView.tag = 101;
[cell.contentView addSubview:myImageView];
}
NSString *myImage = [imageArray objectAtIndex:indexPath.row];
NSString *ImageString = [myImage stringByReplacingOccurrencesOfString:@"" withString:@"%20"];
NSLog(@"Image String %@",ImageString);
NSURL *imageURL = [NSURL URLWithString:ImageString];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImageView *img = (UIImageView*)[cell.contentView viewWithTag:101];
img.backgroundColor = [UIColor redColor ];
cell.backgroundView = [UIImage imageWithData:imageData];
return cell;
}
// Json 服务
NSDictionary *get = @{@"uid":[uidSave valueForKey:@"uid"],@"pro":proNumber};
NSLog(@"Dictionary Data which is to be get %@",get);
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:get options:kNilOptions error:nil];
NSString *jsonInputString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *post = [[NSString alloc]initWithFormat:@"r=%@",jsonInputString];
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@",displayCaseUrl]];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:120.0];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *responseData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (responseData != nil)
{
jsonDict = (NSMutableDictionary *)[NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSLog(@"Values =======%@",jsonDict);
NSMutableDictionary *imageDict = [jsonDict valueForKey:@"images"];
NSLog(@"Image Dictionary :- %@",imageDict);
imageArray = [imageDict valueForKey:@"image_url"];
NSLog(@"MY Array Image :- %@",imageArray);
// 要获取图像的 Json 响应
Image Dictionary :- (
{
"image_url" = "http://zapponomics.net/claimservice/parcelimage/1486154806image0.jpg";
"img_for" = "Front view of parcel";
"pro_number" = orange;
}
)
2015-08-26 10:09:05.806 ClaimCloud[1997:87342] MY Array String :- (
"http://zapponomics.net/claimservice/parcelimage/1486154806image0.jpg"
)
//////// USIng 像这样但是不行
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
UIImageView *myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(50,0, 50, 80)];
myImageView.tag = 101;
[cell.contentView addSubview:myImageView];
NSString *myImage = [imageArray objectAtIndex:indexPath.row];
[self loadImageFromURL:[NSURL URLWithString:[myImage stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] callback:^(UIImage *image) {
myImageView.image = image;
}];
return cell;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(0, 0, 0,200);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(100,80);
}
- (void) loadImageFromURL: (NSURL*) url callback:(void (^)(UIImage *image))callback {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSData * imageData = [NSData dataWithContentsOfURL:url];
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *image = [UIImage imageWithData:imageData];
callback(image);
});
});
}
【问题讨论】: