2016-07-09 24 views
1

Ich benutze imageView.af_setImageWithURL(URL) und es funktioniert gut. Ich bin nicht mit Bild Filter etwas zu tun, und ich sah diese Notiz:Wie kann die Speicherkapazität des NSURLCache mit AlmofireImage am einfachsten auf Null gesetzt werden?

Wenn Sie keine Bildfilter verwenden, ist es, die Speicherkapazität des NSURLCache auf Null zu setzen wird empfohlen, im Speicher zu vermeiden, den gleichen Inhalt zu speichern zweimal.

Was ist der sauberste/kleinste Weg, dies mit der UIImage-Erweiterung zu erreichen?

Antwort

2

Ihre Antwort wird sicherlich funktionieren. Hier ist ein weiterer Ansatz, der nicht erfordert, dass Sie Ihre eigene ImageDownloader erstellen.

let downloader = UIImageView.af_sharedImageDownloader 
let configuration = downloader.sessionManager.session.configuration 
configuration.URLCache?.memoryCapacity = 0 

Dies wird am Ende das gleiche tun, ohne neue Instanzen von etwas zu erstellen.

0

Ich versuche folgendes:

// From https://github.com/Alamofire/AlamofireImage - "If you do not use image filters, it is advised to set the memory capacity of the NSURLCache to zero to avoid storing the same content in-memory twice." 
// Note: We're making an educated guess that NSURLCache does not allocate the memory capacity when it's created.. it just uses it as an upper bound. So we'll let ImageDownloader create the session configuration it wants, then we'll just set the memory capacity on its url cache to 0 as suggested. 
let configurationWithNoMemoryCapacityOnURLCache = ImageDownloader.defaultURLSessionConfiguration() 
configurationWithNoMemoryCapacityOnURLCache.URLCache?.memoryCapacity = 0 

UIImageView.af_sharedImageDownloader = ImageDownloader(
    configuration: configurationWithNoMemoryCapacityOnURLCache 
)