2021-06-16 12:32:48 +02:00
|
|
|
//
|
|
|
|
// PlaceholderImageCacheService.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-6-16.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import AlamofireImage
|
|
|
|
|
|
|
|
final class PlaceholderImageCacheService {
|
|
|
|
|
|
|
|
let cache = NSCache<Key, UIImage>()
|
|
|
|
|
|
|
|
func image(color: UIColor, size: CGSize, cornerRadius: CGFloat = 0) -> UIImage {
|
|
|
|
let key = Key(color: color, size: size, cornerRadius: cornerRadius)
|
|
|
|
guard let image = cache.object(forKey: key) else {
|
|
|
|
var image = UIImage.placeholder(size: size, color: color)
|
|
|
|
if cornerRadius < size.width * 0.5 {
|
|
|
|
image = image
|
|
|
|
.af.imageAspectScaled(toFill: size)
|
|
|
|
.af.imageRounded(withCornerRadius: cornerRadius, divideRadiusByImageScale: false)
|
|
|
|
} else {
|
|
|
|
image = image.af.imageRoundedIntoCircle()
|
|
|
|
}
|
|
|
|
cache.setObject(image, forKey: key)
|
|
|
|
return image
|
|
|
|
}
|
|
|
|
|
|
|
|
return image
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension PlaceholderImageCacheService {
|
2021-06-17 07:45:16 +02:00
|
|
|
class Key: NSObject {
|
2021-06-16 12:32:48 +02:00
|
|
|
let color: UIColor
|
|
|
|
let size: CGSize
|
|
|
|
let cornerRadius: CGFloat
|
|
|
|
|
|
|
|
init(color: UIColor, size: CGSize, cornerRadius: CGFloat) {
|
|
|
|
self.color = color
|
|
|
|
self.size = size
|
|
|
|
self.cornerRadius = cornerRadius
|
|
|
|
}
|
|
|
|
|
2021-06-17 07:45:16 +02:00
|
|
|
override func isEqual(_ object: Any?) -> Bool {
|
|
|
|
guard let object = object as? Key else { return false }
|
|
|
|
return object.color == color
|
|
|
|
&& object.size == size
|
|
|
|
&& object.cornerRadius == cornerRadius
|
2021-06-16 12:32:48 +02:00
|
|
|
}
|
|
|
|
|
2021-06-17 07:45:16 +02:00
|
|
|
override var hash: Int {
|
|
|
|
return color.hashValue ^
|
|
|
|
size.width.hashValue ^
|
|
|
|
size.height.hashValue ^
|
|
|
|
cornerRadius.hashValue
|
2021-06-16 12:32:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|