Created
April 27, 2017 08:51
-
-
Save dodikk/2e1a2811eee00af5ea02004d640292bf to your computer and use it in GitHub Desktop.
AlamofireImage integration for AsyncDisplayKit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AfnImageCacheForAsdk.swift | |
// | |
// Created by Alexander Dodatko on 4/26/17. | |
// | |
import Foundation | |
import AlamofireImage | |
import AsyncDisplayKit | |
public class AfnImageCacheForAsdk: NSObject, ASImageCacheProtocol | |
{ | |
public func cachedImage(with URL: URL, | |
callbackQueue: DispatchQueue, | |
completion: @escaping AsyncDisplayKit.ASImageCacherCompletion) | |
{ | |
let result = self.synchronouslyFetchedCachedImage(with: URL) | |
callbackQueue.async | |
{ | |
completion(result) | |
} | |
} | |
public func clearFetchedImageFromCache(with URL: URL) | |
{ | |
let imageCache: AlamofireImage.ImageRequestCache? = | |
AlamofireImage.ImageDownloader.default.imageCache | |
let request = URLRequest(url: URL) | |
_ = imageCache?.removeImage(for: request, withIdentifier: nil) | |
} | |
public func synchronouslyFetchedCachedImage(with URL: URL) -> ASImageContainerProtocol? | |
{ | |
let imageCache: AlamofireImage.ImageRequestCache? = | |
AlamofireImage.ImageDownloader.default.imageCache | |
let request = URLRequest(url: URL) | |
let result = imageCache?.image(for: request, withIdentifier: nil) | |
return result | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AfnImageLoaderForAsdk.swift | |
// | |
// Created by Alexander Dodatko on 4/26/17. | |
// | |
import Foundation | |
import AlamofireImage | |
import AsyncDisplayKit | |
public class AfnImageLoaderForAsdk: NSObject, ASImageDownloaderProtocol | |
{ | |
private var _pendingRequestMap: [NSUUID : AlamofireImage.RequestReceipt] = [:] | |
private var _lock = NSLock() | |
public func cancelImageDownload(forIdentifier downloadIdentifier: Any) | |
{ | |
let taskId = downloadIdentifier as! NSUUID | |
if let receipt = self.receptForTaskWithId(taskId) | |
{ | |
AlamofireImage.ImageDownloader.default.cancelRequest(with: receipt) | |
} | |
self.removeReceptForTaskWithId(taskId) | |
} | |
public func downloadImage(with URL: URL, | |
callbackQueue: DispatchQueue, | |
downloadProgress: AsyncDisplayKit.ASImageDownloaderProgress?, | |
completion: @escaping AsyncDisplayKit.ASImageDownloaderCompletion) | |
-> Any? | |
{ | |
let request = AfnWebImageRequest(path: URL.absoluteString) | |
let downloadTaskId = NSUUID() | |
let maybePendingRequestHandle: AlamofireImage.RequestReceipt? = | |
AlamofireImage.ImageDownloader.default.download(request) | |
{ | |
(response) in | |
if let loadedImage = response.result.value | |
{ | |
self.removeReceptForTaskWithId(downloadTaskId) | |
completion(loadedImage, nil, downloadTaskId) | |
} | |
} | |
if let pendingRequestHandle = maybePendingRequestHandle | |
{ | |
self.addReceipt(pendingRequestHandle, forTaskWithId: downloadTaskId) | |
return downloadTaskId | |
} | |
else | |
{ | |
return nil | |
} | |
} | |
private func addReceipt(_ receipt: AlamofireImage.RequestReceipt, | |
forTaskWithId downloadTaskId: NSUUID) | |
{ | |
self._lock.lock() | |
self._pendingRequestMap[downloadTaskId] = receipt | |
self._lock.unlock() | |
} | |
private func removeReceptForTaskWithId(_ downloadTaskId: NSUUID) | |
{ | |
self._lock.lock() | |
self._pendingRequestMap.removeValue(forKey: downloadTaskId) | |
self._lock.unlock() | |
} | |
private func receptForTaskWithId(_ downloadTaskId: NSUUID) -> AlamofireImage.RequestReceipt? | |
{ | |
self._lock.lock() | |
let result = self._pendingRequestMap[downloadTaskId] | |
self._lock.unlock() | |
return result | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ChatRoomView: NMessengerViewController | |
{ | |
// ... | |
// ... | |
// storing as ivars since AsyncDisplayKit absorbs weak references | |
private let _imageLoader = AfnImageLoaderForAsdk() | |
private let _imageCache = AfnImageCacheForAsdk() | |
fileprivate func buildNodeForTextMessage(_ message: ChatMessage) -> GeneralMessengerCell | |
{ | |
// ... | |
// ... | |
let imageLoader = self._imageLoader | |
let imageCache = self._imageCache | |
let avatar = ASNetworkImageNode( | |
cache: imageCache, | |
downloader: imageLoader) | |
avatar.defaultImage = UIImage(named: "no-avatar-icon") | |
avatar.url = avatarUrl | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment