Celestial 0.8.130

Celestial 0.8.130

Chrishon Wyllie 维护。



Celestial 0.8.130

  • ChrishonWyllie

Celestial

CI Status Version License Platform

Celestial 是一个内置的缓存管理器,它允许您轻松地缓存视频和图片。您可以使用内置的 UIView 类 URLImageViewURLVideoPlayerView 来快速显示缓存图像和视频。这两个 UIView 类提供了灵活的选项,例如确定缓存的图像或视频将存储在哪里:内存中或本地文件系统中。



在这个小演示中,滚动浏览需要不断重新获取每个图像,这会导致冗余的 API 调用和闪烁单元格的 UI 问题

目录


先决条件

  • Xcode 8.0 或更高版本
  • iOS 10.0 或更高版本

安装

Celestial可通过CocoaPods获取。要安装它,只需将以下行添加到您的Podfile中

pod 'Celestial'

使用

图像缓存

为缓存图像,请使用URLImageView,它是默认的UIImageView的子类。URLImageView可以用指向图像的URL进行初始化,也可以手动加载此图像。

第一个初始化程序接受一个sourceURLString: String,它是图像文件所在URL的绝对字符串。这两个初始化程序共享2个参数

  • 代理:URLCachableViewDelegate会通知接收器诸如下载完成、进度、错误等事件。
  • 缓存位置:ResourceCacheLocation决定了下载的图像完成后将存储在何处。默认情况下,图像存储在inMemory中。使用此设置存储的图像或视频不在应用程序会话间持久化,并在系统内存紧张时可能会自动删除。使用.fileSystem将图像持久化至应用程序会话间,直到手动删除。但是,对于图像,内存缓存通常是足够的。设置.none表示不缓存
import Celestial

let sourceURLString = <your URL string>
let imageView = URLImageView(sourceURLString: sourceURLString, delegate: nil, cacheLocation: .inMemory)

// Will automatically load the image from the sourceURLString, and cache the downloaded image
// in a local NSCache, for reuse later



单元格中的图像缓存...

在UICollectionViewCells和UITableViewCells中缓存图像略有不同。在这种情况下,需要首先初始化URLImageView,而urlString可能稍后提供,当cell被 dequeued时。

在这种情况下,请使用func loadImageFrom(urlString:)函数。

struct CellModel {
    let urlString: String
}

class ImageCell: UICollectionViewCell {

    // Initialize the URLImageView within the cell as a variable.
    // NOTE: The second initializer is used which does NOT have the urlString argument.
    private var imageView: URLImageView = {
        let img = URLImageView(delegate: nil, cacheLocation: .inMemory)
        img.translatesAutoresizingMaskIntoConstraints = false
        return img
    }()
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(imageView)
        
        // Handle layout...
    }
    
    // This function is called during the cell dequeue process and will load the image
    // using the `CellModel` struct. However, this would be replaced with your method.
    public func configureCell(someCellModel: CellModel) {
        imageView.loadImageFrom(urlString: someCellModel.urlString)
    }

}

监听下载事件

下载数据图像和视频时可能会发生三种事件

  • 下载成功完成
  • 下载正在进行中
  • 下载图像或视频时发生错误

URLImageViewURLVideoPlayerView提供不同的方式来监听这些事件

使用委托

扩展URLCachableViewDelegate以接收这些事件通知

let sourceURLString = <your URL string>
let imageView = URLImageView(sourceURLString: sourceURLString, delegate: self, cacheLocation: .inMemory)

...


extension ViewController: URLCachableViewDelegate {
    
    func urlCachableView(_ view: URLCachableView, didFinishDownloading media: Any) {
        // Image has finished downloading and will be cached to specified cache location
    }
    
    func urlCachableView(_ view: URLCachableView, downloadFailedWith error: Error) {
        // Investigate download error
    }
    
    func urlCachableView(_ view: URLCachableView, downloadProgress progress: Float, humanReadableProgress: String) {
        // Update UI with download progress if necessary
    }
}

使用完成块

另一种方法是使用内联完成块接收这些事件

let imageView = URLImageView(delegate: nil, cacheLocation: .inMemory, defaultImage: nil)

let sourceURLString = <your URL string>

imageView.loadImageFrom(urlString: sourceURLString,
progressHandler: { (progress) in
    print("current downlod progress: \(progress)")
}, completion: {
    print("Image has finished loading")
}) { (error) in
    print("Error loading image: \(error)")
}



缓存视频

与缓存和显示图像类似,URLVideoPlayerView将显示视频并在之后将其缓存以供重新使用。它封装了通常繁琐的创建AVAssetAVPlayerItemAVPlayerLayerAVPlayer实例的过程。相反,你只需提供一个URL即可播放。

如果你已经阅读了缓存图像部分,则URLImageViewURLVideoPlayerView之间的初始化器和函数几乎相同。

let sourceURLString = <your URL string>
let videoView = URLVideoPlyerView(sourceURLString: sourceURLString, delegate: nil, cacheLocation: .fileSystem)

在此示例中,视频将播放并缓存到本地文件系统。注意将该内容缓存在本地系统将保留图像或视频,直到手动删除为止。

在单元中缓存视频

如前所述,URLVideoPlayerView 中提供的功能几乎与 URLImageView 的功能相同

public func configureCell(someCellModel: CellModel) {
    playerView.loadVideoFrom(urlString: someCellModel.urlString)
}

struct CellModel {
    let urlString: String
}

class VideoCell: UICollectionViewCell {

    private var playerView: URLVideoPlayerView = {
        // Observe events with delegation...
        let v = URLVideoPlayerView(delegate: self, cacheLocation: .fileSystem)
        v.translatesAutoresizingMaskIntoConstraints = false
        v.playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
        return v
    }()
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(playerView)
        
        // Handle layout...
    }
    
    // This function is called during the cell dequeue process and will load the image
    // using the `CellModel` struct. However, this would be replaced with your method.
    public func configureCell(someCellModel: CellModel) {
        
        // Or with completion handlers...
        
        playerView.loadVideoFrom(urlString: someCellModel.urlString, 
        progressHandler: { (progress) in
            print("current downlod progress: \(progress)")
        }, completion: {
            print("Video has finished loading")
        }) { (error) in
            print("Error loading video")=
        }
    }
}

完整文档

查看完整文档

示例

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

作者

ChrishonWyllie,[email protected]

许可

Celestial 遵循 MIT 许可证。欲了解更多信息,请参阅 LICENSE 文件。