Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Collectionview不显示JSON解析图像_Ios_Json_Uicollectionview - Fatal编程技术网

Ios Collectionview不显示JSON解析图像

Ios Collectionview不显示JSON解析图像,ios,json,uicollectionview,Ios,Json,Uicollectionview,我是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"] @int

我是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;
}
这里我的自定义收藏视图单元格图像视图是gallery图像


然后,我的图像已从web服务解析,但未显示在“收藏视图”单元中。请为我提供正确的解决方案。

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 ImageName:img2]

您可能应该使用具有UIImageView属性的自定义UICollectionViewCell类,使用与UICOllectionView的帧相同的帧初始化它

然后在viewController的cellForRow方法中,使用SDWebImage异步设置单元格的自定义照片

单元头文件 单元实现文件 查看控制器实现文件 然后将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];

    }];
}

当图像加载到占位符图像的位置时,我想在集合视图单元格中放置一个微调器,这是可能的吗?如果可能的话,请告诉我这是如何可能的。
@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"]];


}
@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
-(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];

    }];
}