Ignore request parameters using SDWebImage

I use SDWebImage to cache images in my application, however, I recently encountered a problem where the images that should be stored in the cache continue to be updated. Looking at it, I found that the full URL of the image from AWS is actually changing due to parameters tied to the end of the URL. Each time I retrieve an object containing the image URL, the image URL is returned with the dynamic parameter “signature” and “expire” (for security purposes). Another url in relation to the image cache, but pay attention to the same image path.

First sample:

https://myapp.s3.amazonaws.com/path/image123.jpeg?AWSAccessKeyId=SOMEKEY&Signature=vrUFlMFEQ9fqQ%3D&Expires=1441702633

Enter another 1 second again:

https://myapp.s3.amazonaws.com/path/image123.jpeg?AWSAccessKeyId=SOMEKEY&Signature=2mcMxUJLyJd7E%3D&Expires=1441703105

What is the best way to deal with this situation? Of course, it would be great if SDWebImage had the ability to ignore request parameters outside the file path.

+7
ios caching amazon-s3 sdwebimage
source share
4 answers

SDWebImage has a method that allows you to use a user key, which helps in this case when AWS modifies the request each time it is called.

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { SDWebImageManager.sharedManager.cacheKeyFilter = ^(NSURL *url) { url = [[NSURL alloc] initWithScheme:url.scheme host:url.host path:url.path]; return [url absoluteString]; }; // Your app init code... return YES; } 

More Details: SDWebImage | Using a cache filter

+10
source share

@ The answer to the question is very good, but I sometimes met with a breakdown.

below is a more stable version.

 SDWebImageManager.sharedManager.cacheKeyFilter = ^(NSURL *url) { if( [[url absoluteString] isEqualToString:@""] ){ return @""; } url = [[NSURL alloc] initWithScheme:url.scheme host:url.host path:url.path]; return [url absoluteString]; }; 

This additional code validates the URL. [[NSURL alloc] initWithString:@""] or something like this leads to crashes.

+6
source share

Answer for Swift language:

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { ............... SDWebImageManager.sharedManager().cacheKeyFilter = { url in if url.absoluteString == "" { return "" } let newUrl = NSURL(scheme: url.scheme, host: url.host, path: url.path!)! return newUrl.absoluteString } return true } 
+5
source share

[NSURL initWithScheme: host: path:] deprecated in iOS10. use NSURLComponents .

my updated solution:

 SDWebImageManager.sharedManager.cacheKeyFilter = ^NSString *(NSURL *url) { if([[url absoluteString] isEqualToString:@""]){ return @""; } NSURLComponents *urlComponents = [[NSURLComponents alloc] initWithURL:url resolvingAgainstBaseURL:NO]; urlComponents.query = nil; return [[urlComponents URL] absoluteString]; }; 
0
source share

All Articles