feat: ios mobil arayüz tasarımı
This commit is contained in:
34
ios/Bookibra/Services/ImageCache.swift
Normal file
34
ios/Bookibra/Services/ImageCache.swift
Normal file
@@ -0,0 +1,34 @@
|
||||
import Foundation
|
||||
import UIKit
|
||||
|
||||
protocol ImageCacheProtocol {
|
||||
func image(for url: URL) async throws -> UIImage
|
||||
}
|
||||
|
||||
final class ImageCache: ImageCacheProtocol {
|
||||
static let shared = ImageCache()
|
||||
|
||||
private let cache = NSCache<NSURL, UIImage>()
|
||||
private let session: URLSession
|
||||
|
||||
init(session: URLSession = .shared) {
|
||||
self.session = session
|
||||
cache.countLimit = 200
|
||||
}
|
||||
|
||||
func image(for url: URL) async throws -> UIImage {
|
||||
if let existing = cache.object(forKey: url as NSURL) {
|
||||
return existing
|
||||
}
|
||||
|
||||
let request = URLRequest(url: url, cachePolicy: .returnCacheDataElseLoad, timeoutInterval: 20)
|
||||
let (data, _) = try await session.data(for: request)
|
||||
|
||||
guard let image = UIImage(data: data) else {
|
||||
throw APIError.invalidResponse
|
||||
}
|
||||
|
||||
cache.setObject(image, forKey: url as NSURL)
|
||||
return image
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user