【发布时间】:2014-10-21 09:15:58
【问题描述】:
我是 iOS 开发的新手。我想从 Web 服务解析图像并显示到集合视图自定义单元格中。然后我写代码就像
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)
#define imageURL [NSURL URLWithString:@"http://www.truemanindiamagazine.com/webservice/gallery_image.php"]
@interface CollectionViewController ()
{
NSData *data;
}
@end
@implementation CollectionViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
dispatch_async(kBgQueue, ^{
data = [NSData dataWithContentsOfURL: imageURL];
[self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});
[self.imagecollection registerNib:[UINib nibWithNibName:@"Custum" bundle:nil] forCellWithReuseIdentifier:@"CellIdentifier"];
}
-(void)fetchedData:(NSData *)responsedata
{
NSError* error;
self.json = [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
self.imagesa=[_json objectForKey:@"data"];
if (self.imagesa.count) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.imagecollection reloadData];
});
}
NSLog(@"images,%@",self.imagesa);
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.imagesa.count;
}
-(Custum *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
Custum *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"CellIdentifier" forIndexPath:indexPath];
NSDictionary *dict = [self.imagesa objectAtIndex:indexPath.item];
NSString *img2=[dict valueForKey:@"link"];
NSLog(@"IMages Name %@",img2);
cell.galleryImage.image=[UIImage imageNamed:img2];
[cell.contentView addSubview:cell.galleryImage];
return cell;
}
这里我的自定义收藏视图单元格图像视图是图库图像。
然后我的图像是从网络服务中解析出来的,但没有显示在集合视图单元格中,请给我正确的解决方案。
【问题讨论】:
标签: ios json uicollectionview