给AsyncDisplayKit(Texture)替换网络图片下载缓存框架
cubegao
2020-08-03 PM
2175℃
0条
前言
为了方便管理图片缓存,统一缓存文件夹和缓存方法,将AsyncDisplayKit自带的PINRemoteImage插件,
替换为项目在用的Kingfisher、SDWebImage。
import Kingfisher
extension ASNetworkImageNode {
static func imageNode() -> ASNetworkImageNode {
return ASNetworkImageNode(cache: ASImageManager.shared, downloader: ASImageManager.shared)
}
}
class ASImageManager: NSObject, ASImageDownloaderProtocol, ASImageCacheProtocol {
static let shared = ASImageManager()
private override init() {}
func downloadImage(with url: URL, callbackQueue: DispatchQueue, downloadProgress: ASImageDownloaderProgress?, completion: @escaping ASImageDownloaderCompletion) -> Any? {
ImageDownloader.default.downloadTimeout = 30.0
var operation: DownloadTask?
operation = ImageDownloader.default.downloadImage(with: url, options: nil, progressBlock: { (received, expected) in
if downloadProgress != nil {
callbackQueue.async(execute: {
let progress = expected == 0 ? 0 : received / expected
downloadProgress?(CGFloat(progress))
})
}
}) { (result) in
switch result {
case .success(let value):
callbackQueue.async(execute: { completion(value.image, nil, nil, nil) })
ImageCache.default.store(value.image, original: value.originalData, forKey: url.cacheKey, toDisk: true)
case .failure(let error):
callbackQueue.async(execute: { completion(nil, error, operation, nil) })
dPrint("Job failed: \(error.localizedDescription)")
}
}
return operation
}
func cancelImageDownload(forIdentifier downloadIdentifier: Any) {
if let task = downloadIdentifier as? DownloadTask {
task.cancel()
}
}
func cachedImage(with url: URL, callbackQueue: DispatchQueue, completion: @escaping ASImageCacherCompletion) {
ImageCache.default.retrieveImage(forKey: url.cacheKey) { (result) in
switch result {
case .success(let value):
callbackQueue.async { completion(value.image) }
case .failure(let error):
dPrint("Job failed: \(error.localizedDescription)")
}
}
}
}
import SDWebImage
extension ASNetworkImageNode {
static func imageNode() -> ASNetworkImageNode {
return ASNetworkImageNode(cache: ASNetImageManage.shared, downloader: ASNetImageManage.shared)
}
}
class ASNetImageManage: NSObject, ASImageDownloaderProtocol, ASImageCacheProtocol {
static let shared = ASNetImageManage()
func downloadImage(with URL: URL, callbackQueue: DispatchQueue, downloadProgress: ASImageDownloaderProgress?, completion: @escaping ASImageDownloaderCompletion) -> Any? {
weak var weakOperation: SDWebImageOperation?
let operation = SDWebImageManager.shared.loadImage(with: URL, options: .retryFailed, progress: { (received, expected, url) in
if downloadProgress != nil {
callbackQueue.async(execute: {
let progress = expected == 0 ? 0 : received / expected
downloadProgress?(CGFloat(progress))
})
}
}) { (cachedImage, data, error, type, unknow, url) in
if let image = cachedImage {
callbackQueue.async(execute: { completion(image, nil, nil, nil) })
return
}
callbackQueue.async(execute: { completion(nil, error, nil, nil) })
}
weakOperation = operation
return weakOperation
}
func cancelImageDownload(forIdentifier downloadIdentifier: Any) {
if let downloadIdentifier = downloadIdentifier as? SDWebImageOperation {
downloadIdentifier.cancel()
}
}
func cachedImage(with URL: URL, callbackQueue: DispatchQueue, completion: @escaping ASImageCacherCompletion) {
if let key = SDWebImageManager.shared.cacheKey(for: URL) {
SDWebImageManager.shared.imageCache.queryImage(forKey: key, options: .allowInvalidSSLCertificates, context: nil) { (cachedImage, data, type) in
if let image = cachedImage {
callbackQueue.async(execute: { completion(image) })
return
}
callbackQueue.async(execute: { completion(nil) })
}
}else {
callbackQueue.async {
completion(nil)
}
}
}
}