Proyectos de Subversion Iphone Microlearning - Nuevo Interface

Rev

Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

//
//  ImageCache.swift
//  twogetskills
//
//  Created by Efrain Yanez Recanatini on 3/9/22.
//


import UIKit

protocol ImageCache {
    subscript(_ key: NSString) -> UIImage? { get set }
}

struct TemporaryImageCache: ImageCache {
    subscript(key: NSString) -> UIImage? {
        get {
            cache.object(forKey: key as NSString)
        }
        set {
            newValue == nil ? cache.removeObject(forKey: key as NSString) : cache.setObject(newValue!, forKey: key as NSString)
        }
    }
    
    private let cache: NSCache<NSString, UIImage> = {
        let cache = NSCache<NSString, UIImage>()
        cache.countLimit = 100 // 100 items
        cache.totalCostLimit = 1024 * 1024 * 600 // 600 MB
        return cache
    }()
    
    /*
    subscript(_ key: String) -> UIImage? {
        get { cache.object(forKey: key as NSString) }
        set { newValue == nil ? cache.removeObject(forKey: key as NSString) : cache.setObject(newValue!, forKey: key as NSString) }
    }
 */
}



/*
protocol ImageCache {
    subscript(_ url: URL) -> UIImage? { get set }
}

struct TemporaryImageCache: ImageCache {
    private let cache: NSCache<NSURL, UIImage> = {
        let cache = NSCache<NSURL, UIImage>()
        cache.countLimit = 100 // 100 items
        cache.totalCostLimit = 1024 * 1024 * 600 // 600 MB
        return cache
    }()
    
    subscript(_ key: URL) -> UIImage? {
        get { cache.object(forKey: key as NSURL) }
        set { newValue == nil ? cache.removeObject(forKey: key as NSURL) : cache.setObject(newValue!, forKey: key as NSURL) }
    }
}
 */