Skip to content

Instantly share code, notes, and snippets.

@hilen
Last active December 4, 2018 18:37
Show Gist options
  • Save hilen/8ef4a810406051e8f0c1 to your computer and use it in GitHub Desktop.
Save hilen/8ef4a810406051e8f0c1 to your computer and use it in GitHub Desktop.
FLAnimatedImageView with SDWebImage
```
import Foundation
import FLAnimatedImage
import SDWebImage
import Kingfisher
/*
Add this repo(https://github.com/rs/SDWebImage) to your project
*/
extension FLAnimatedImageView {
private struct AssociatedKeys {
static var isGifKey = "isGifKey"
static var imageURLStringKey = "imageURLStringKey"
static var imageDiskPathPathKey = "imageDiskPathPathKey"
}
//http://stackoverflow.com/questions/17828044/is-it-possible-to-find-the-file-extension-of-a-uiimage
public var isGIF: Bool {
get { return objc_getAssociatedObject(self, &AssociatedKeys.isGifKey) as? Bool ?? false }
set { objc_setAssociatedObject(self, &AssociatedKeys.isGifKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)}
}
public var imageURLString: String {
get { return objc_getAssociatedObject(self, &AssociatedKeys.imageURLStringKey) as? String ?? "" }
set { objc_setAssociatedObject(self, &AssociatedKeys.imageURLStringKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN) }
}
public var imageDiskPath: String {
get { return objc_getAssociatedObject(self, &AssociatedKeys.imageDiskPathPathKey) as? String ?? "" }
set { objc_setAssociatedObject(self, &AssociatedKeys.imageDiskPathPathKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN) }
}
private func URLEncoded(string URLString: String) -> NSURL? {
guard let URLEncodingString = URLString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()) else {
return nil
}
return NSURL(string: URLEncodingString)
}
public func fla_cancelCurrentImageLoad() {
self.sd_cancelCurrentImageLoad()
}
public func fla_setImageWithURLString(URLString: String?, placeholder: UIImage? = nil) {
self.image = placeholder!
guard let URLString = URLString, imageURL = NSURL(string: URLString) ?? URLEncoded(string: URLString) else {
print(" URL Error")
return
}
self.imageURLString = URLString
self.isGIF = self.imageURLString.lowercaseString.hasSuffix(".gif")
let manager = SDWebImageManager.sharedManager()
let memoryImage = manager.imageCache.imageFromMemoryCacheForKey(URLString)
let diskImage = manager.imageCache.imageFromDiskCacheForKey(URLString)
guard let cacheImage = memoryImage ?? diskImage else {
self.fla_downloadImageWithURL(imageURL)
return
}
self.imageDiskPath = manager.imageCache.defaultCachePathForKey(URLString)
let imageData = NSData(contentsOfFile: self.imageDiskPath)
guard imageData != nil else {
print("data is nil")
return
}
guard let finalURL = self.imageURLString as String? else {
return
}
if finalURL == URLString {
self.setImageSource(imageData!, image: cacheImage)
}
}
/**
progress: { (recvSize: Int, totalSize: Int) -> Void in
if recvSize < totalSize {
// _ = Int( CGFloat(recvSize) * 100.0 / CGFloat(totalSize) )
} else if recvSize == totalSize {
}
}
*/
private func fla_downloadImageWithURL(URL: NSURL) {
let downloader: SDWebImageDownloader = SDWebImageDownloader.sharedDownloader()
downloader.downloadImageWithURL(URL,
options: SDWebImageDownloaderOptions.ProgressiveDownload,
progress: nil,
completed: {[weak self] (image: UIImage!, originalData: NSData!, error: NSError!, finished : Bool) in
guard error == nil && finished else {
return
}
guard image != nil || originalData != nil else {
return
}
guard let newImage = image, newOriginalData = originalData else {
return
}
//When object is nil , return
if (self == nil) {
return
}
guard let finalURL = self!.imageURLString as String? else {
return
}
SDWebImageManager.sharedManager().imageCache.storeImage(
newImage,
recalculateFromImage: false,
imageData: newOriginalData,
forKey: finalURL,
toDisk: true)
if finalURL == URL.absoluteString {
self!.setImageSource(originalData, image: image)
}
}
)
}
private func setImageSource(GIFData: NSData? = nil, image: UIImage? = nil) {
dispatch_async(dispatch_get_main_queue(), {
if self.isGIF {
self.animatedImage = FLAnimatedImage(animatedGIFData: GIFData)
self.image = nil
} else {
self.animatedImage = nil
self.image = image
}
})
}
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment