mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			58 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Swift
		
	
		
		
			
		
	
	
			58 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Swift
		
	
| 
											8 years ago
										 | // | ||
|  | //  Copyright (c) 2017 Open Whisper Systems. All rights reserved. | ||
|  | // | ||
|  | 
 | ||
|  | import Foundation | ||
|  | import UIKit | ||
|  | 
 | ||
|  | class ImageCacheRecord: NSObject { | ||
|  |     var variations: [CGFloat: UIImage] | ||
|  |     init(variations: [CGFloat: UIImage]) { | ||
|  |         self.variations = variations | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * A two dimensional hash, allowing you to store variations under a single key. | ||
| 
											8 years ago
										 |  * This is useful because we generate multiple diameters of an image, but when we | ||
| 
											8 years ago
										 |  * want to clear out the images for a key we want to clear out *all* variations. | ||
|  |  */ | ||
|  | @objc | ||
| 
											8 years ago
										 | public class ImageCache: NSObject { | ||
| 
											8 years ago
										 | 
 | ||
|  |     let backingCache: NSCache<AnyObject, ImageCacheRecord> | ||
|  | 
 | ||
| 
											8 years ago
										 |     public override init() { | ||
| 
											8 years ago
										 |         self.backingCache = NSCache() | ||
|  |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     @objc | ||
|  |     public func image(forKey key: AnyObject, diameter: CGFloat) -> UIImage? { | ||
| 
											8 years ago
										 |         guard let record = backingCache.object(forKey: key) else { | ||
|  |             return nil | ||
|  |         } | ||
|  |         return record.variations[diameter] | ||
|  |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     @objc | ||
|  |     public func setImage(_ image: UIImage, forKey key: AnyObject, diameter: CGFloat) { | ||
| 
											8 years ago
										 |         if let existingRecord = backingCache.object(forKey: key) { | ||
|  |             existingRecord.variations[diameter] = image | ||
|  |             backingCache.setObject(existingRecord, forKey: key) | ||
|  |         } else { | ||
|  |             let newRecord = ImageCacheRecord(variations: [diameter: image]) | ||
|  |             backingCache.setObject(newRecord, forKey: key) | ||
|  |         } | ||
|  |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     @objc | ||
|  |     public func removeAllImages() { | ||
| 
											8 years ago
										 |         backingCache.removeAllObjects() | ||
|  |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     @objc | ||
|  |     public func removeAllImages(forKey key: AnyObject) { | ||
| 
											8 years ago
										 |         backingCache.removeObject(forKey: key) | ||
|  |     } | ||
|  | } |