BLLazyImageLoad 0.1.2

BLLazyImageLoad 0.1.2

测试测试
语言语言 Obj-CObjective C
许可 MIT
发布最后一个版本2016年10月

tianfire 维护。



  • 作者:
  • tianfire

BLLazyImageLoad 是一个 iOS 插件类,当滚动视图拖动或减速结束时显示 tableViewcollectionView 中的图片。
img_demo01.gif

要求

BLLazyImageLoad 在 iOS 8+ 上运行并需要 ARC 来构建。它依赖于以下 Apple 框架,这些框架应该已经包含在大多数 Xcode 模板中:

  • SDWebImage

您需要最新的开发者工具来构建 BLLazyImageLoad。旧版本的 Xcode 可能可以使用,但不会明确维护兼容性。

将 BLLazyImageLoad 添加到您的项目中

源文件

或者,您可以直接将 LazyImageLoad/ 源文件添加到您的项目中。

  1. 下载最新的代码版本 latest code version 或将存储库作为 git 子模块添加到您的 git 追踪项目中。
  2. 在 Xcode 中打开您的项目,然后将 LazyImageLoad/ 拖放到您的项目上(使用“Product Navigator view”,在询问是否引用外部项目提取的代码存档时,请确保选择 Copy 项)。
  3. 使用 #import "UIScrollView+lazyImage.h" 在需要 BLLazyImageLoad 的地方包含它。

如何使用

1: 引入头文件

#import "UIScrollView+lazyImage.h"

2: 设置懒加载数据源

__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;
    }];

3: 设置单元格的懒加载图片

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = ...
    [tableView setLazyImageForCell:cell indexPath:indexPath];
    return cell;
}

4: 设置滚动视图代理

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if (!decelerate && scrollView.dataForIndexPathBlock) {
        [scrollView loadImageForForVisibleItems];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.dataForIndexPathBlock) {
        [scrollView loadImageForForVisibleItems];
    }
}

联系方式

https://cgfloat.com
[email protected]