2021-06-16 12:32:48 +02:00
|
|
|
//
|
|
|
|
// BlurhashImageCacheService.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-6-16.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
|
2022-01-29 12:51:40 +01:00
|
|
|
public final class BlurhashImageCacheService {
|
|
|
|
|
2022-05-11 12:39:34 +02:00
|
|
|
// MARK: - Singleton
|
|
|
|
public static let shared = BlurhashImageCacheService()
|
|
|
|
|
2022-01-29 12:51:40 +01:00
|
|
|
static let edgeMaxLength: CGFloat = 20
|
2021-06-16 12:32:48 +02:00
|
|
|
|
|
|
|
let cache = NSCache<Key, UIImage>()
|
|
|
|
|
|
|
|
let workingQueue = DispatchQueue(label: "org.joinmastodon.app.BlurhashImageCacheService.working-queue", qos: .userInitiated, attributes: .concurrent)
|
|
|
|
|
2022-01-29 12:51:40 +01:00
|
|
|
public func image(
|
|
|
|
blurhash: String,
|
|
|
|
size: CGSize,
|
|
|
|
url: String
|
|
|
|
) -> AnyPublisher<UIImage?, Never> {
|
2021-06-17 07:45:16 +02:00
|
|
|
let key = Key(blurhash: blurhash, size: size, url: url)
|
|
|
|
|
|
|
|
if let image = self.cache.object(forKey: key) {
|
|
|
|
return Just(image).eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
2021-06-16 12:32:48 +02:00
|
|
|
return Future { promise in
|
|
|
|
self.workingQueue.async {
|
2022-01-29 12:51:40 +01:00
|
|
|
guard let image = BlurhashImageCacheService.blurhashImage(blurhash: blurhash, size: size) else {
|
2021-06-17 07:45:16 +02:00
|
|
|
promise(.success(nil))
|
2021-06-16 12:32:48 +02:00
|
|
|
return
|
|
|
|
}
|
2021-06-17 07:45:16 +02:00
|
|
|
self.cache.setObject(image, forKey: key)
|
2021-06-16 12:32:48 +02:00
|
|
|
promise(.success(image))
|
|
|
|
}
|
|
|
|
}
|
2021-06-17 07:45:16 +02:00
|
|
|
.receive(on: RunLoop.main)
|
2021-06-16 12:32:48 +02:00
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
2022-01-29 12:51:40 +01:00
|
|
|
static func blurhashImage(blurhash: String, size: CGSize) -> UIImage? {
|
|
|
|
let imageSize: CGSize = {
|
|
|
|
let aspectRadio = size.width / size.height
|
|
|
|
if size.width > size.height {
|
|
|
|
let width: CGFloat = BlurhashImageCacheService.edgeMaxLength
|
|
|
|
let height = width / aspectRadio
|
|
|
|
return CGSize(width: width, height: height)
|
|
|
|
} else {
|
|
|
|
let height: CGFloat = BlurhashImageCacheService.edgeMaxLength
|
|
|
|
let width = height * aspectRadio
|
|
|
|
return CGSize(width: width, height: height)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
let image = UIImage(blurHash: blurhash, size: imageSize)
|
|
|
|
|
|
|
|
return image
|
2021-06-16 12:32:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension BlurhashImageCacheService {
|
2021-06-17 07:45:16 +02:00
|
|
|
class Key: NSObject {
|
2021-06-16 12:32:48 +02:00
|
|
|
let blurhash: String
|
|
|
|
let size: CGSize
|
2022-01-29 12:51:40 +01:00
|
|
|
let url: String
|
2021-06-16 12:32:48 +02:00
|
|
|
|
2022-01-29 12:51:40 +01:00
|
|
|
init(blurhash: String, size: CGSize, url: String) {
|
2021-06-16 12:32:48 +02:00
|
|
|
self.blurhash = blurhash
|
|
|
|
self.size = size
|
|
|
|
self.url = url
|
|
|
|
}
|
|
|
|
|
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.blurhash == blurhash
|
|
|
|
&& object.size == size
|
|
|
|
&& object.url == url
|
|
|
|
}
|
|
|
|
|
|
|
|
override var hash: Int {
|
|
|
|
return blurhash.hashValue ^
|
|
|
|
size.width.hashValue ^
|
|
|
|
size.height.hashValue ^
|
|
|
|
url.hashValue
|
2021-06-16 12:32:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|