将图像解析为ios中的Collection-view Custom单元格

时间:2014-10-14 05:23:35

标签: ios json uicollectionview

我想从Web服务解析图像并显示到集合视图自定义单元格中,我将代码编写为

在我的.h文件中

@property(strong,nonatomic)IBOutlet UICollectionView *imagecollection;
@property(strong,nonatomic)NSArray *imagesa;
@property(strong,nonatomic)NSDictionary *json;
@property(strong,nonatomic)NSArray *aimages;

和我的.m文件

#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)


#define imageURL [NSURL URLWithString:@"http://www.truemanindiamagazine.com/webservice/gallery_image.php"]

- (void)viewDidLoad
{
[super viewDidLoad];
dispatch_async(kBgQueue, ^{
    data = [NSData dataWithContentsOfURL: imageURL];
    [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});

[self.imagecollection registerNib:[UINib nibWithNibName:@"Custumcell" bundle:nil] forCellWithReuseIdentifier:@"CellIdentifier"];
}
-(void)fetchedData:(NSData *)responsedata
{
NSError* error;
self.json = [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
self.imagesa=[json objectForKey:@"data"];
NSLog(@"images,%@",self.imagesa);
}

 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.imagesa.count;
}
-(Custumcell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
 {
Custumcell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"CellIdentifier" forIndexPath:indexPath];
UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(0,0,100,100)];
NSString *img2=[self.imagesa objectAtIndex:indexPath.row];
img.image=[UIImage imageNamed:img2];
cell.imageview.image=[UIImage imageNamed:img2];
return cell;
}

然后解析来自Web服务的图像,但没有显示到集合视图中,请给我任何解决方案。

2 个答案:

答案 0 :(得分:2)

尝试替换

-(void)fetchedData:(NSData *)responsedata
{
NSError* error;
self.json = [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
self.imagesa=[json objectForKey:@"data"];
NSLog(@"images,%@",self.imagesa);
}

代码

-(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(), ^{
                [imagecollection reloadData];
            });
      }
NSLog(@"images,%@",self.imagesa);
}

现在使用SDWebImageDownloader并在cellForRowAtIndexpath方法中,将方法cellForRowAtIndexPath替换为

Custumcell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"CellIdentifier" forIndexPath:indexPath];
    NSDictionary *dict = [self.imagesa objectAtIndex:indexPath.item];
    NSString *img2=[dict valueForKey:@"link"];
    [cell.imageview sd_setImageWithURL:[NSURL URLWithString:[img2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[UIImage imageNamed:@"temp.png"] options:SDWebImageProgressiveDownload completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"downloaded");
        });
    }];
    return cell;

还会在您的文件中导入#import "UIImageView+WebCache.h"

可能会对你有帮助。

答案 1 :(得分:0)

#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
-(Custumcell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
Custumcell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"CellIdentifier" forIndexPath:indexPath];
cell.imageview.image= nil; // or cell.poster.image = [UIImage imageNamed:@"placeholder.png"];

dispatch_async(kBgQueue, ^{
    NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[self.imagesa objectAtIndex:indexPath.row] objectForKey:@"link"]]];
    if (imgData) {
        UIImage *image = [UIImage imageWithData:imgData];
        if (image) {
            dispatch_async(dispatch_get_main_queue(), ^{
                Custumcell *updateCell = (id)[collectionView cellForItemAtIndexPath:indexPath];
                if (updateCell)
                    updateCell.imageview.image = image;
            });
        }
    }
});
return cell;
}