You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.7 KiB
92 lines
2.7 KiB
//
|
|
// FLAnimatedImage+SetImage.swift
|
|
// elpha-ios
|
|
//
|
|
// Created by Dwayne Harris on 11/12/18.
|
|
// Copyright © 2018 Elpha. All rights reserved.
|
|
//
|
|
|
|
import Alamofire
|
|
import AlamofireImage
|
|
import FLAnimatedImage
|
|
import Foundation
|
|
|
|
class ImageCache {
|
|
static var cache: NSCache<AnyObject, AnyObject> = NSCache<AnyObject, AnyObject>()
|
|
|
|
static func set(string: String, data: Data) {
|
|
ImageCache.cache.setObject(data as AnyObject, forKey: string as AnyObject)
|
|
}
|
|
|
|
static func set(url: URL, data: Data) {
|
|
set(string: url.absoluteString, data: data)
|
|
}
|
|
|
|
static func set(request: URLRequest, data: Data) {
|
|
set(url: request.url!, data: data)
|
|
}
|
|
|
|
static func get(string: String) -> Data? {
|
|
if let data = ImageCache.cache.object(forKey: string as AnyObject) as? Data {
|
|
return data
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
static func get(url: URL) -> Data? {
|
|
return get(string: url.absoluteString)
|
|
}
|
|
|
|
static func get(request: URLRequest) -> Data? {
|
|
return get(url: request.url!)
|
|
}
|
|
}
|
|
|
|
extension FLAnimatedImageView {
|
|
func setPlaceholder() {
|
|
self.contentMode = .center
|
|
self.image = UIImage(named: "Help")
|
|
}
|
|
|
|
func setImage(withURL url: URL, withShadow shadow: Bool = false, withContentMode contentMode: UIView.ContentMode = .scaleAspectFill) {
|
|
func setShadow() {
|
|
if shadow {
|
|
self.layer.shadowColor = UIColor.black.cgColor
|
|
self.layer.shadowOpacity = 0.8
|
|
self.layer.shadowOffset = CGSize.zero
|
|
self.layer.shadowRadius = 10
|
|
}
|
|
}
|
|
|
|
if let data = ImageCache.get(url: url) {
|
|
self.contentMode = contentMode
|
|
setShadow()
|
|
|
|
if url.absoluteString.hasSuffix(".gif") {
|
|
self.animatedImage = FLAnimatedImage(animatedGIFData: data)
|
|
} else {
|
|
self.image = UIImage(data: data)
|
|
}
|
|
} else {
|
|
setPlaceholder()
|
|
|
|
Alamofire.request(url).responseImage { response in
|
|
if let data = response.data {
|
|
DispatchQueue.main.async {
|
|
ImageCache.set(url: url, data: data)
|
|
|
|
self.contentMode = contentMode
|
|
setShadow()
|
|
|
|
if url.absoluteString.hasSuffix(".gif") {
|
|
self.animatedImage = FLAnimatedImage(animatedGIFData: data)
|
|
} else {
|
|
self.image = UIImage(data: data)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|