我是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;
}
此处我的自定义集合视图单元格图像视图是图库图片。
然后我的图像是从Web服务解析但未显示在集合视图单元格中,请给我正确的解决方案。
答案 0 :(得分:2)
您应该使用具有UIImageView属性的自定义UICollectionViewCell类,使用与UICOllectionView框架相同的框架初始化它。
然后在viewController的cellForRow方法中,使用SDWebImage异步设置单元格的自定义照片。
@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"]];
}
修改自定义单元格类以包含新的UIActivitiyIndicator属性:
@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];
}];
}
答案 1 :(得分:0)
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];