【问题标题】:Collectionview not show JSON Parsing imageCollectionview 不显示 JSON 解析图像
【发布时间】: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


    【解决方案1】:

    您可能应该使用具有 UIImageView 属性的自定义 UICollectionViewCell 类,并使用与 UICollectionView 的框架相同的框架对其进行初始化。

    然后在你的 viewController 的 cellForRow 方法中,使用 SDWebImage 异步设置你的 cell 的自定义照片。

    单元格头文件

    @interface CustomCell: UICollectionViewCell
    
    @property (nonatomic, strong) UIImageView *photo;
    
    @end
    

    单元实现文件

    @implementation CustomCell
    
    -(id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
    
        if(self)
        {
            self.photo = [[UIImageView alloc] initWithFrame:frame];
            self.photo.contentMode = UIViewContentModeScaleAspectFill;
    
            [self.contentView addSubview:self.photo];
        }
    
        return self;
    }
    
    @end
    

    查看控制器实现文件

    #import "CustomCell.h"
    #import <SDWebImage/UIImageView+WebCache.h>
    
    -(void)viewDidLoad
    {
        ...
        self.collectionView = [[UICollectionView alloc] init....];
        self.collectionView registerClass:[CustomCell class] forCellWithReuseIdentifier:@"cellID"];
    }
    
    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
    
        // using SDWebImage to download photo asynchronously.
    
        [cell.photo sd_setImageWithURL:[NSURL urlWithString:[json valueForKey:@"imageURL"]] placeholderImage:[UIImage imageNamed:@"placeholderImage.png"]];
    
    
    }
    

    微调器

    修改您的自定义单元格类以包含新的 UIActivitiyIndi​​cator 属性:

    @interface CustomCell: UICollectionViewCell
    
    @property (nonatomic, strong) UIImageView *photo;
    @property (nonatomic, strong) UIActivityIndicatorView *spinner;
    
    @end
    
    @implementation CustomCell
    
    -(id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
    
        if(self)
        {
            self.photo = [[UIImageView alloc] initWithFrame:frame];
            self.photo.contentMode = UIViewContentModeScaleAspectFill;
    
            self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
            // might or might not need to set frame of spinner
    
            [self.contentView addSubview:self.photo];
            [self.photo addSubview:self.spinner];
        }
    
        return self;
    }
    
    @end
    

    然后将 SDWebImage 调用更改为带有完成块的调用:

    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
    
        // using SDWebImage to download photo asynchronously.
    
        [cell.spinner startAnimating];    
    
        [cell.photo sd_setImageWithURL:[NSURL urlWithString:[json valueForKey:@"imageURL"]] placeholderImage:[UIImage imageNamed:@"placeholderImage.png"] options:0 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { 
    
            // tell spinner to stop animating after image finished downloading
            [cell.spinner stopAnimating];
    
        }];
    }
    

    【讨论】:

    • 当图像加载到占位符图像的位置时,我想在集合视图单元格中放置一个微调器,这是可能的吗?如果可能的话,请告诉我这是怎么可能的。
    【解决方案2】:

    img2是图片的链接,所以尝试添加:

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        NSData *imageData = [NSData dataWithContentsOfURL:img2];
        dispatch_async(dispatch_get_main_queue(), ^{
            cell.galleryImage.image = [UIImage imageWithData:imageData];
        });
    });
    

    而不是cell.galleryImage.image=[UIImage imageNamed:img2];

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-25
      • 1970-01-01
      相关资源
      最近更新 更多