BLLazyImageLoad
是一个 iOS 插件类,当滚动视图拖动或减速结束时显示 tableView
或 collectionView
中的图片。
BLLazyImageLoad
在 iOS 8+ 上运行并需要 ARC 来构建。它依赖于以下 Apple 框架,这些框架应该已经包含在大多数 Xcode 模板中:
您需要最新的开发者工具来构建 BLLazyImageLoad
。旧版本的 Xcode 可能可以使用,但不会明确维护兼容性。
或者,您可以直接将 LazyImageLoad/
源文件添加到您的项目中。
LazyImageLoad/
拖放到您的项目上(使用“Product Navigator view”,在询问是否引用外部项目提取的代码存档时,请确保选择 Copy 项)。#import "UIScrollView+lazyImage.h"
在需要 BLLazyImageLoad 的地方包含它。#import "UIScrollView+lazyImage.h"
__weak __typeof(self)weakSelf = self;
[self.tableView setImageSourceBlocks:^NSObject *(NSIndexPath *indexPath) {
// -- Return model of tableview datasource.(返回表视图数据源中此indexPath对应的对象)
return weakSelf.dataSource[indexPath.row];
} imgBlock:^id(NSIndexPath *indexPath) {
// -- Return imageView's property name of model Class.(返回imageView在数据源model类型中的属性名字)
DemoData *data = weakSelf.dataSource[indexPath.row];
if (data.imgURLs.count < 3) {
// -- If only One image,return "string".(单张图片返回返回字符串类型)
return @"imgView";
} else {
// -- If more than one images, return [array].(多张图片返回数组类型)
return @[@"imgView1", @"imgView2", @"imgView3"];
}
} urlblock:^id(NSIndexPath *indexPath) {
// -- If only One image, Return string of image's url.
DemoData *data = weakSelf.dataSource[indexPath.row];
if (data.imgURLs.count < 3) {
return data.imgURLs[0];
} else {
// -- If more than one images, Return [array].
return data.imgURLs;
}
} placeholderBlock:^id(NSIndexPath *indexPath) {
// -- Return placeholder.(-> UIImage)
return nil;
}];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = ...
[tableView setLazyImageForCell:cell indexPath:indexPath];
return cell;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
if (!decelerate && scrollView.dataForIndexPathBlock) {
[scrollView loadImageForForVisibleItems];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if (scrollView.dataForIndexPathBlock) {
[scrollView loadImageForForVisibleItems];
}
}