ImageStore 0.6.0

ImageStore 0.6.0

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最新发布2019年10月
SPM支持 SPM

miup 维护。



  • miup

ImageStore

CI Status Version License Platform

ImageStore 是一个支持内存缓存的图像下载器。

示例

要运行示例项目,请首先克隆仓库,然后从 Example 目录运行 pod install

需求

  • Swift 3.2
  • iOS 10.0 或更高
  • cocoapods 1.4.0 或更高

安装

ImageStore 通过CocoaPods 提供使用。要安装它,只需在 Podfile 中添加以下行:

pod "ImageStore"

类、结构体、扩展

ImageStoreConfig

ImageStore配置结构体。

属性

  • maxDownloadSize
    一张图片的最大下载大小。(默认2MB)

  • cacheLimit
    缓存大小。(默认200MB)

方法

  • init(maxDownloadSize: Int64, cacheLimit: Int64)

ImageStore

图片下载类。

属性

  • shared: ImageStore (静态)
    单例共享实例。

  • config: ImageStoreConfig
    ImageStore的配置。

  • completionsByURLString: [String: [ImageStoreCompletionHandler]]
    以源URL String为索引的完成处理器字典。

  • downloadTaskByURLString: [String: URLSessionDownloadTask]
    以源URL String为索引的下载任务字典。

  • cache: NSCache
    图片缓存。

  • queue: OperationQueue
    图片下载队列。

  • session: URLSession
    图片下载URLSession。

方法

  • reset(config: ImageStoreConfig) (类)
    从 ImageStoreConfig 创建新的共享实例。

  • load(_ url: URL, completion: ImageStoreCompletionHandler?)
    从 URL 下载图像,并执行 CompletionHandler。如果已缓存,则仅执行 CompletionHandler。
    如果下载任务已暂停,则恢复它。

let url = URL(string: "your.images.com/0.jpg")

ImageStore.shared.load(url) { [weak self] image in
    self?.myImageView.image = image
}

ImageStore.shared.load(url) { [weak self] image in
    self?.mySecondImageView.image = image
}

如果你这样写,则仅创建一个下载任务。但执行两个 CompletionHandler。

  • suspendIfResuming(url: URL)
    如果 ImageStore 有带有参数 URL 的下载任务且正在恢复,则暂停它。
    你可以通过 load(url) 函数来恢复它。

  • cancel(url: URL)
    删除具有参数 URL 的下载任务。

UIImageView+ImageStore

用于 ImageStore 的 UIImageView 扩展。

方法

  • load(_ url: URL, placeholderImage: UIImage?, shouldSetImageConditionBlock: @escaping (() -> Bool))
    第二个参数 shouldSetImageConditionBlock 是返回一个条件的闭包,允许 ImageView 显示图像。
    它适用于可重复使用的视图(例如 UITableViewCell, UICollectionViewCell)。
    当这些视图被重用时,你可能会改变单元格的 ImageView 的图像。
    你必须在每个单元格的 prepareForReuse 函数中调用 suspendIfResuming(url: URL)cancel(url: URL)
    但是 suspendIfResuming(url: URL)cancel(url: URL) 并不会删除用于恢复或后续下载的 completion handler。
    因此,如果在 prepareForReuse 和下一个下载图像 completion handler 之间执行 completion handler,则之前显示的图像将一直显示,直到下一个下载完成。
    请看以下示例。
class MyTableViewCell: UITableViewCell {
    static let cellIdentifier = "MyTableViewCell"
    var id: String?
    var myImageView: UIImageView = UIImageView()

    override func prepareForReuse() {
        super.prepareForReuse()
        id = nil
        myImageView.image = nil
        myImageView.suspendLoading()
    }
}

class MyListViewController: UIViewController, UITableViewDataSource {
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: MyTableViewCell.cellIdentifier, for: indexPath) as? MyViewCell else { return UITableViewCell() }
        let url = urls[indexPath.row]
        let urlString = url.absoluteString
        cell.id = urlString
        cell.myImageView.load(url) {
            // display image if cell's id and loading image url string is match.
            return cell.id == urlString
        }

        return cell
    }
}
  • cancelLoading()
    取消当前 URL 加载。

  • suspendLoading()
    暂停当前 URL 加载。

作者

miup, [email protected]

许可

ImageStore 采用 MIT 许可证。更多信息请参阅 LICENSE 文件。

贡献

  • 如果您发现一个错误,请打开一个问题报告。
  • 我正在等待您的功能请求,请打开一个问题报告。
  • 我正在等待您的贡献,请创建一个新的拉取请求。