CNOSAdImgDownloadManager.swift
2.43 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
//
// CNOSAdImgDownloadManager.swift
// metaCode
//
// Created by zx on 2025/4/22.
//
import Foundation
import SDWebImage
import YYImage
final class CNOSAdImgDownloadManager {
static let shared = CNOSAdImgDownloadManager()
private var cache: SDImageCache!
private init() {
let paths = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)
let cachePath = paths.first!.appendingPathComponent("CNLiveOpenScreen").path
cache = SDImageCache(namespace: "AdImage", diskCacheDirectory: cachePath)
}
/// 下载广告图片
static func downloadOSAdImg(with url: String?, success: (() -> Void)? = nil) {
guard let validURL = url?.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed),
let imageURL = URL(string: validURL) else {
return
}
SDWebImageDownloader.shared.downloadImage(with: imageURL, options: [.ignoreCachedResponse]) { receivedSize, expectedSize, targetURL in
} completed: { image, data, error, finished in
guard error == nil, finished, let image = image else {
return
}
let ocUrl: NSString = NSString(string: url ?? "")
if let md5Key = ocUrl.md5(){
// guard let data = image.imageData else { return }//image.sd_imageData()
// shared.cache.storeImageData(toDisk: data, forKey: md5Key)
shared.cache.store(image, forKey: md5Key, completion: {
success?()
})
}
}
}
/// 获取缓存图片
static func getOSImage(with url: String?) -> UIImage? {
let ocUrl: NSString = NSString(string: url ?? "")
if let md5Key = ocUrl.md5(){
// let isCached = shared.cache.diskImageDataExists(withKey: md5Key)
//
// if isCached{
// return shared.cache.diskImageData(forKey: md5Key)
// }
return shared.cache.imageFromCache(forKey: md5Key)// as? YYImage
}
return shared.cache.imageFromCache(forKey: "")// as? YYImage
}
/// 清除指定图片缓存
static func clearOSImage(with url: String?) {
let ocUrl: NSString = NSString(string: url ?? "")
if let md5Key = ocUrl.md5(){
// shared.cache.removeImageFromDisk(forKey: md5Key)
shared.cache.removeImage(forKey: md5Key, withCompletion: nil)
}
}
}