Skip to content

YogendraShelke/ImageCache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 

Repository files navigation

ImageCache

let imageCache = NSCache<AnyObject, AnyObject>()

func associatedObject<ValueType: AnyObject>( base: AnyObject, key: UnsafePointer<UInt8>, initialiser: () -> ValueType) -> ValueType {
            if let associated = objc_getAssociatedObject(base, key) as? ValueType { return associated }
            let associated = initialiser()
            objc_setAssociatedObject(base, key, associated, .OBJC_ASSOCIATION_RETAIN)
            return associated
}
func associateObject<ValueType: AnyObject>( base: AnyObject, key: UnsafePointer<UInt8>, value: ValueType) {
    	objc_setAssociatedObject(base, key, value, .OBJC_ASSOCIATION_RETAIN)
}

private var urlKey: UInt8 = 0

class URLString {
    	var urlString = String()
}

extension UIImageView {
    
    	var imageURL: URLString {
            	get {
                    	return associatedObject(base: self, key: &urlKey)
                    	{ return URLString() }
            	}
            	set { associateObject(base: self, key: &urlKey, value: newValue) }
    	}
    
    	func loadImageUsingUrlString(urlString: String) {
            	imageURL.urlString = urlString
            	guard let url = URL(string: urlString) else { return }
            	image = nil
            	if let imageFromCache = imageCache.object(forKey: urlString as AnyObject) as? UIImage {
                    	self.image = imageFromCache
                    	return
            	}
            
            	URLSession.shared.dataTask(with: url, completionHandler: { (data, respones, error) in
                    	if error != nil { return }
                    	if let imgData = data {
                            	DispatchQueue.main.async(execute: {
                                    	let imageToCache = UIImage(data: imgData)
                                    	if self.imageURL.urlString == urlString {
                                            	self.image = imageToCache
                                    	}
                                    	if let imageToBeCached = imageToCache {
                                            	imageCache.setObject(imageToBeCached, forKey: urlString as AnyObject)
                                    	}
                            	})
                    	}
            	}).resume()
    	}
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published