[SWIFT] SDWebImage: How to clear the cache for a particular UIImageView

Introduction

SDWebImage, which is widely used as an image cache library, I think that there are many scenes that mainly use the following methods as methods when clearing the cache.

SDImageCache.sharedImageCache().clearMemory()
SDImageCache.sharedImageCache().clearDisk()

But if you want to clear the cache for a particular UIImageView, It is expensive to clear all the caches one by one, so I tried to find out how to clear the cache of a specific UIImageView.

Clear cache for a particular UIImageView

This can be achieved by using the following method.

SDImageCache.shared.removeImageFromDisk(forKey: String)
SDImageCache.shared.removeImageFromDisk(forKey: String)

The argument forKey (cacheKey) seems to be the URL set in the UIImageView. ▼ Official Document

The cache key is an application unique identifier for the image to cache. It is generally the absolute URL of the image.

I'm a little worried about generally, but lol (probably because you can specify the key when caching?) I was able to clear the cache by implementing as follows.

if let cacheKey = uiimageView.sd_imageURL?.absoluteString {
    SDImageCache.shared.removeImageFromDisk(forKey: cacheKey)
    SDImageCache.shared.removeImageFromDisk(forKey: cacheKey)
}

Recommended Posts

SDWebImage: How to clear the cache for a particular UIImageView
How to make a mod for Slay the Spire
How to check for the contents of a java fixed-length string
How to create a Maven repository for 2020
How to create pagination for a "kaminari" array
[For beginners] How to implement the delete function
How to make a lightweight JRE for distribution
[Java] (for MacOS) How to set the classpath
How to run the SpringBoot app as a service
[Rails] How to create a signed URL for CloudFront
How to use an array for a TreeMap key
How to write a unit test for Spring Boot 2
[Spring Boot] How to create a project (for beginners)
How to make a cache without thinking too much
How to study kotlin for the first time ~ Part 2 ~
How to study kotlin for the first time ~ Part 1 ~
How to take a screenshot with the Android Studio emulator
How to create a lightweight container image for Java apps
How to create a form to select a date from the calendar
How to leave a comment
How to create a placeholder part to use in the IN clause
How to test a private method with RSpec for yourself
[Ruby] How to retrieve the contents of a double hash
How to output array values without using a for statement
How to add the same Indexes in a nested array
Minecraft Modding [1.12] How to attach a special render for Item
A memorandum to reach the itchy place for Java Gold
[jsoup] How to get the full amount of a document
How to insert a video
How to create a method
How to create and launch a Dockerfile for Payara Micro
How to get the latest live stream ID for a channel without using the YouTube Data API
[Java] How to search for a value in an array (or list) with the contains method
How to deal with the type that I thought about writing a Java program for 2 years
Mechanism for converting to a language that the browser can recognize
How to create a jar file or war file using the jar command
I want to create a chat screen for the Swift chat app!
How to make a hinadan for a Spring Boot project using SPRING INITIALIZR
How to create a registration / update function where the table crosses
How to get the contents of Map using for statement Memorandum
[Rails] How to change the page title of the browser for each page
How to run a Kotlin Coroutine sample from the command line
[Rails] How to put a crown mark on the ranking function
How to store the information entered in textarea in a variable in the method
How to identify the path that is easy to make a mistake
[chown] How to change the owner of a file or directory
[For beginners] How to get the Ruby delayed railway line name
kotlin & Java: How to hide the toolbar only for specific fragments
How to make a groundbreaking diamond using Java for statement wwww
How to use the link_to method
How to use the include? method
How to add columns to a table
How to use the form_with method
How to specify validation for time_field
How to install JMeter for Mac
How to find the average angle
How to make a Java container
How to use the wrapper class
How to sign a Minecraft MOD
How to make a JDBC driver
[Java] How to create a folder