cubegao

给AsyncDisplayKit(Texture)替换网络图片下载缓存框架

2020-08-03

前言

为了方便管理图片缓存,统一缓存文件夹和缓存方法,将AsyncDisplayKit自带的PINRemoteImage插件,
替换为项目在用的Kingfisher、SDWebImage。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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)")
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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)
}
}

}
}

扫描二维码,分享此文章