CSLazyLoadController 1.0.1

CSLazyLoadController 1.0.1

测试已测试
语言语言 Obj-CObjective C
许可 MIT
发布最新发布2014年12月

Josip Bernat 维护。



  • Clover 开发者和 Josip Bernat

CSLazyLoadController 帮助您在 iOS 应用中集成懒加载图像。

Podfile

platform :ios, '6.0'
pod 'CSLazyLoadController', '~> 1.0'

用法

创建属性并遵守 CSLazyLoadControllerDelegate 协议

    @interface CSViewController () <CSLazyLoadControllerDelegate>

    @property (nonatomic, strong) CSLazyLoadController *lazyLoadController;

    @end

在您的 init 中创建实例

    // I.e. initWithCoder
    - (id)initWithCoder:(NSCoder *)aDecoder {

        if (self = [super initWithCoder:aDecoder]) {

            self.lazyLoadController = [[CSLazyLoadController alloc] init];
            self.lazyLoadController.delegate = self;
        }
        return self;
    }

当您 deque cells 时设置图像

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

        static NSString *cellIdentifier = @"CellIdentifier";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        cell.textLabel.text = [NSString stringWithFormat:@"Row: %ld", (long)indexPath.row];

        NSString *stringUrl = self.items[indexPath.row]; // Some NSString instance holding url to the image.

        UIImage *image = [self.lazyLoadController fastCacheImage:[CSURL URLWithString:stringUrl]]; // Find image in RAM memory.
        cell.imageView.image = image; // Update the image in cell
        // If there is not image download it
        if (!image && !tableView.dragging) {
            [self.lazyLoadController startDownload:[CSURL URLWithString:stringUrl parameters:[self parameters] method:CSHTTPMethodPOST]
                                      forIndexPath:indexPath];
        }

    return cell;
    }

响应用户滚动视图代理

    // Once scrollView stops with scrolling we want to load images for visible rows
    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

        if ([scrollView isEqual:_tableView] && !decelerate) {
            [_lazyLoadController loadImagesForOnscreenRows:[_tableView indexPathsForVisibleRows]];
        }
    }

    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

        if ([scrollView isEqual:_tableView]){
            [_lazyLoadController loadImagesForOnscreenRows:[_tableView indexPathsForVisibleRows]];
        }
    }

响应用户 CSLazyLoadController 代理

    // Controller asks us for URL so give him image URL
    - (CSURL *)lazyLoadController:(CSLazyLoadController *)loadController
        urlForImageAtIndexPath:(NSIndexPath *)indexPath {

        NSString *stringUrl = self.items[indexPath.row];
        return [CSURL URLWithString:stringUrl];
    }

    // Image has finished with downloading so update the cell
    - (void)lazyLoadController:(CSLazyLoadController *)loadController
                didReciveImage:(UIImage *)image
                        fromURL:(CSURL *)url
                        indexPath:(NSIndexPath *)indexPath {

        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
        cell.imageView.image = image;
        [cell setNeedsLayout];
    }