异步下载图片,动态设置cell高度
2017-05-10 09:52
267 查看
在网上找到了一段不错的代码,笔记一下
#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.imgArray.count;//图片URL以数组的形式存在
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// 先从缓存中查找图片
UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey: self.imgArray[indexPath.row]];
// 没有找到已下载的图片就使用默认的占位图,当然高度也是默认的高度了,除了高度不固定的文字部分。
if (!image) {
image = [UIImage imageNamed:kDownloadImageHolder];
}
//手动计算cell
CGFloat imgHeight = image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width;
return imgHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *imgID = @"pictureCellID";
PYClubPresentDetailImgTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:imgID];
if (nil == cell) {
cell = [[PYClubPresentDetailImgTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:imgID];
}
[self configureCell:cell atIndexPath:indexPath];
cell.userInteractionEnabled = NO;
return cell;
}
- (void)configureCell:(PYClubPresentDetailImgTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
NSString *imgURL = self.imgArray[indexPath.row];
UIImage *cachedImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:imgURL];
if ( !cachedImage ) {
[self downloadImage:self.imgArray[indexPath.row] forIndexPath:indexPath];
[cell.btn setBackgroundImage:[UIImage imageNamed:kDownloadImageHolder] forState:UIControlStateNormal];
} else {
[cell.btn setBackgroundImage:cachedImage forState:UIControlStateNormal];
}
}
- (void)downloadImage:(NSString *)imageURL forIndexPath:(NSIndexPath *)indexPath {
// 利用 SDWebImage 框架提供的功能下载图片
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:imageURL] options:SDWebImageDownloaderUseNSURLCache progress:^(NSInteger receivedSize, NSInteger expectedSize) {
// do nothing
} completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
[[SDImageCache sharedImageCache] storeImage:image forKey:imageURL toDisk:YES];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}];
}
#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.imgArray.count;//图片URL以数组的形式存在
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// 先从缓存中查找图片
UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey: self.imgArray[indexPath.row]];
// 没有找到已下载的图片就使用默认的占位图,当然高度也是默认的高度了,除了高度不固定的文字部分。
if (!image) {
image = [UIImage imageNamed:kDownloadImageHolder];
}
//手动计算cell
CGFloat imgHeight = image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width;
return imgHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *imgID = @"pictureCellID";
PYClubPresentDetailImgTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:imgID];
if (nil == cell) {
cell = [[PYClubPresentDetailImgTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:imgID];
}
[self configureCell:cell atIndexPath:indexPath];
cell.userInteractionEnabled = NO;
return cell;
}
- (void)configureCell:(PYClubPresentDetailImgTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
NSString *imgURL = self.imgArray[indexPath.row];
UIImage *cachedImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:imgURL];
if ( !cachedImage ) {
[self downloadImage:self.imgArray[indexPath.row] forIndexPath:indexPath];
[cell.btn setBackgroundImage:[UIImage imageNamed:kDownloadImageHolder] forState:UIControlStateNormal];
} else {
[cell.btn setBackgroundImage:cachedImage forState:UIControlStateNormal];
}
}
- (void)downloadImage:(NSString *)imageURL forIndexPath:(NSIndexPath *)indexPath {
// 利用 SDWebImage 框架提供的功能下载图片
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:imageURL] options:SDWebImageDownloaderUseNSURLCache progress:^(NSInteger receivedSize, NSInteger expectedSize) {
// do nothing
} completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
[[SDImageCache sharedImageCache] storeImage:image forKey:imageURL toDisk:YES];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}];
}
相关文章推荐
- 利用NSOperation进行异步图片下载——设置UITabView数据,图片下载,占位图。解决异步下载和Cell重用造成的图片设置混乱。和多次重复下载的问题
- Android 根据图片高度动态设置ViewPager高度
- 最简单动态设置不同cell高度的方法。
- TableViewCell自适应高度(异步下载)
- 几种设置UITableView的cell动态高度的方法
- 动态设置图片的宽度和高度
- 总结了几种设置UITableView的cell动态高度的方法
- 微信小程序 动态的设置图片的高度和宽度详解及实例代码
- 关于Xib使用AutoLayout动态设置cell高度
- ios 动态设置Cell的高度
- 在异步下载图片进行剪切的时候如何动态的获得imageview的大小进行剪切
- 微信小程序图片等比例缩放 动态的获取图片的高度和宽度 动态的设置图片的高度和宽度
- 动态设置Cell的高度
- python3抓取异步百度瀑布流动态图片(二)get、json下载代码讲解
- 轮播图动态设置高度保持图片不变形
- UITableView 异步加载图片,cell自适应高度!项目常用。
- 几种设置UITableView的cell动态高度的方法
- 用flash代替图片IMG,设置动态效果链接
- Flex动态设置Button中的icon指向外网图片源
- 设置Discuz 7论坛游客可以查看图片,但不能下载附件.