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) } }
} }
|