iOS开发之UICollectionViewDataSourcePrefetching

简介: 在iOS10中,苹果为UICollectionViewCell引入了Pre-Fetching预加载机制用于提升它的性能。主要引入了一个新的数据源协议UICollectionViewDataSourcePrefetching,包含两个方法:@proto...

在iOS10中,苹果为UICollectionViewCell引入了Pre-Fetching预加载机制用于提升它的性能。主要引入了一个新的数据源协议UICollectionViewDataSourcePrefetching,包含两个方法:

@protocol UICollectionViewDataSourcePrefetching <NSObject>
@required
// 预加载数据
- (void)collectionView:(UICollectionView *)collectionView prefetchItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths NS_AVAILABLE_IOS(10_0);
@optional
// 取消提前加载数据
- (void)collectionView:(UICollectionView *)collectionView cancelPrefetchingForItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths  NS_AVAILABLE_IOS(10_0);
@end

网上搜了一大圈,讲述原理的(翻译文档)的文章很多,有干货的Demo很少,于是乎自己摸索了一下,写了一个简单的案例,在此记录并分享一下。

运行环境:Xcode 8.2.1 + iOS 10.2

核心步骤:

1、遵从 UICollectionViewDataSourcePrefetching 协议
2、实现 collectionView:prefetchItemsAtIndexPaths 方法和collectionView:cancelPrefetchItemsAtIndexPaths 方法(可选)
3、将第1步中遵从协议的类设置为 UICollectionView 的 prefetchDataSource 属性

实现

一、创建UICollectionViewFlowLayout

自己写一个类继承自UICollectionViewFlowLayout

@implementation MyCollectionViewFlowLayout

-(void)prepareLayout{

    self.minimumLineSpacing = 1;//垂直间距
    self.minimumInteritemSpacing = 0;//水平间距
    self.sectionInset = UIEdgeInsetsMake(0, 0, 8, 0);//分组间距

}
@end

二、用XIB定义一个

里面就一个UIImageView,然后拽线设置一个IBOutlet


img_2e9b38821af2c3d2186c5ed9b286b452.png
UICollectionViewCell.png
@property (weak, nonatomic) IBOutlet UIImageView *imgView;

三、控制器

注释很详细

#import "ViewController.h"
#import "MyCollectionViewFlowLayout.h"
#import "ImgCollectionViewCell.h"

#define ScreenW [UIScreen mainScreen].bounds.size.width
//重用标识
static NSString *cellId = @"imgCell";
//遵守协议
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDataSourcePrefetching>

//下载图片任务
@property(nonatomic, strong) NSMutableDictionary<NSURL *, dispatch_queue_t> *tasks;
//图片地址
@property(nonatomic, copy) NSMutableArray<NSURL *> *imgURLArray;
//下载的图片
@property(nonatomic, copy) NSMutableDictionary<NSURL *, UIImage *> *imgs;
//UICollectionView
@property(nonatomic, weak) UICollectionView *collectionView;

@end

@implementation ViewController

//懒加载imgURLArray
-(NSMutableArray<NSURL *> *)imgURLArray{
    
    if (_imgURLArray == nil) {   
        _imgURLArray = [NSMutableArray array];    
        for (int i = 0; i < 30; i++) {
            NSURL *imgURL = [NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1494499175005&di=1d8d40ac84f4a71cb26d7bf4a5a845ec&imgtype=0&src=http%3A%2F%2Fimg10.360buyimg.com%2Fyixun_zdm%2Fjfs%2Ft2830%2F11%2F2310606472%2F165925%2F962fa94a%2F575f7664Nfd743845.jpg"];
            [_imgURLArray addObject:imgURL];            
        }
    }
    
    return _imgURLArray;    
}

//懒加载imgs
-(NSMutableDictionary<NSURL *,UIImage *> *)imgs{
    if (_imgs == nil) {        
        _imgs = [NSMutableDictionary dictionary];
    }   
    return _imgs;
}

//懒加载tasks
-(NSMutableDictionary<NSURL *,dispatch_queue_t> *)tasks{
    if (_tasks == nil) {       
        _tasks = [NSMutableDictionary dictionary];
    }   
    return _tasks;   
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //创建UICollectionView
    //创建布局
    UICollectionViewLayout *layout = [[MyCollectionViewFlowLayout alloc]init];
    //初始化一个UICollectionView
    UICollectionView *collection = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
    //设置背景色
    collection.backgroundColor = [UIColor groupTableViewBackgroundColor];
    //设置代理
    collection.dataSource = self;
    collection.delegate = self;
    collection.prefetchDataSource = self;
    //注册Cell
    UINib *nib = [UINib nibWithNibName:@"ImgCollectionViewCell" bundle:nil];
    [collection registerNib:nib forCellWithReuseIdentifier:cellId];
     
    [self.view addSubview:collection];
    
    self.collectionView = collection;
    
}


-(void)loadImage:(NSIndexPath *)indexPath{
    
    NSURL *currentURL = [self.imgURLArray objectAtIndex:indexPath.row];
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);  
    __weak typeof(self) weakSelf = self;
    
    //异步下载图片
    dispatch_async(queue, ^{
      
        NSData *imageData = [NSData dataWithContentsOfURL:currentURL];
        UIImage *image = [UIImage imageWithData:imageData];
        weakSelf.imgs[currentURL] = image;
        
        //更新UI   
         dispatch_async(dispatch_get_main_queue(), ^{
            
            ImgCollectionViewCell *cell = (ImgCollectionViewCell *)[weakSelf.collectionView cellForItemAtIndexPath:indexPath];
            cell.imgView.image = image;
        });
    });
    
    //为了取消任务
    self.tasks[currentURL] = queue;
    
}

#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.imgURLArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
     
    ImgCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    
    // 获取URL
    NSURL *imgURL = self.imgURLArray[indexPath.row];
    
    //对应的URL的图片已经存在
    if (self.imgs[imgURL]) {        
        cell.imgView.image = self.imgs[imgURL];
    }
    //不存在
    else{
        
        [self loadImage:indexPath];
    }
    
    return cell;
    
}


#pragma mark <UICollectionViewDelegate>
//定义每个Item 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 
    CGFloat W = (ScreenW - 1) / 2;    
    return CGSizeMake(W, 100);
    
}

#pragma mark <UICollectionViewDataSourcePrefetching>
- (void)collectionView:(UICollectionView *)collectionView prefetchItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths {
    
    for (NSIndexPath * indexPath in indexPaths) {
        
        NSURL *currentURL = [self.imgURLArray objectAtIndex:indexPath.row]; 
        //不存在就请求
        if (!self.imgs[currentURL]) {
            [self loadImage:indexPath];
        }
    }
    
}


- (void)collectionView:(UICollectionView *)collectionView cancelPrefetchingForItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths {
    
    for (NSIndexPath * indexPath in indexPaths){
        
        NSURL *currentURL = [self.imgURLArray objectAtIndex:indexPath.row];  
        //当前任务存在
        if (self.tasks[currentURL]) {
            dispatch_queue_t queue = self.tasks[currentURL];
            dispatch_suspend(queue);
            self.tasks[currentURL] = nil;
        }
    }
    
}

@end

效果

img_2708c27e640a433c1b67450826a998f9.gif
效果演示.gif

写在后面的话

1、这个新特性仍然需要探究
2、遇到的一个坑:细心看的话可以发现我的字典是懒加载的,如果直接在viewDidLoad中初始化会在 weakSelf.imgs[currentURL] = image; 一行报错,why?烦请知道的告知。

源代码

目录
相关文章
|
1月前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
85 3
|
3月前
|
存储 iOS开发
iOS 开发,如何进行应用的本地化(Localization)?
iOS 开发,如何进行应用的本地化(Localization)?
122 2
|
3月前
|
存储 数据建模 数据库
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
39 0
|
3月前
|
安全 编译器 Swift
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
91 2
|
3月前
|
API 开发工具 iOS开发
iOS 开发高效率工具包:10 大必备工具
iOS 开发高效率工具包:10 大必备工具
47 1
|
3月前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
54 1
|
6天前
|
API 定位技术 iOS开发
IOS开发基础知识:什么是 Cocoa Touch?它在 iOS 开发中的作用是什么?
【4月更文挑战第18天】**Cocoa Touch** 是iOS和Mac OS X应用的核心框架,包含面向对象库、运行时系统和触摸优化工具。它提供Mac验证的开发模式,强调触控接口和性能,涵盖3D图形、音频、网络及设备访问API,如相机和GPS。是构建高效iOS应用的基础,对开发者至关重要。
11 0
|
21天前
|
开发工具 Swift iOS开发
利用SwiftUI构建动态用户界面:iOS开发新范式
【4月更文挑战第3天】 随着苹果不断推进其软件开发工具的边界,SwiftUI作为一种新兴的编程框架,已经逐渐成为iOS开发者的新宠。不同于传统的UIKit,SwiftUI通过声明式语法和强大的功能组合,为创建动态且响应式的用户界面提供了一种更加简洁高效的方式。本文将深入探讨如何利用SwiftUI技术构建具有高度自定义能力和响应性的用户界面,并展示其在现代iOS应用开发中的优势和潜力。
|
2月前
|
监控 API Swift
用Swift开发iOS平台上的上网行为管理监控软件
在当今数字化时代,随着智能手机的普及,人们对于网络的依赖日益增加。然而,对于一些特定场景,如家庭、学校或者企业,对于iOS设备上的网络行为进行管理和监控显得尤为重要。为了满足这一需求,我们可以利用Swift语言开发一款iOS平台上的上网行为管理监控软件。
196 2
|
3月前
|
数据可视化 iOS开发
iOS 开发,什么是 Interface Builder(IB)?如何使用 IB 构建用户界面?
iOS 开发,什么是 Interface Builder(IB)?如何使用 IB 构建用户界面?
40 4