PINRemoteImage icon indicating copy to clipboard operation
PINRemoteImage copied to clipboard

Allow providing a custom cache key which overrides the url in pin_setImage(from: URL?)

Open MilesV64 opened this issue 2 years ago • 4 comments

My server sends lots of signed URLs which means the PIN cache downloads new images every time since the signed URL is slightly different. I'd like to use a custom key that I pass in, and then the library would only use the URL for downloading the image, not for use as the cache key.

It could look like this:

// The latest signed url from my server
let myURL = self.imageURL 

// A string uniquing the image; if this changes then it means the image changed. 
// Otherwise it's the same image but could be a different URL due to signed URLs.
let id = self.imageID

// CacheKey would be optional so current library behavior is unchanged
// The library would then use the given id as the cache key, only using `myURL` if it needs to be downloaded
self.imageView.pin_setImage(from: myURL, cacheKey: id) 

Would this be possible? Is there any other way to get around handling lots of signed URLs without redownloading every time?

MilesV64 avatar Apr 28 '23 15:04 MilesV64

Can you override imageFromCacheWithCacheKey? https://github.com/pinterest/PINRemoteImage/blob/18637e4414d77fbc95cd7d499179a9880e9a78d8/Source/Classes/include/PINRemoteImageManager.h#L596-L605

garrettmoon avatar Apr 28 '23 16:04 garrettmoon

Thank you for the reply, I think that should be enough to go on! I like the idea of setting the key at the same time as setting the URL because this override method requires additional state management/parsing of the URL (unless I'm missing a better way), but thank you for the suggestion.

To use this properly I would subclass PINRemoteImageManager correct? How would I then ensure that is being used instead of .shared() when calling pin_setImage() on a UIImageView?

MilesV64 avatar Apr 28 '23 18:04 MilesV64

Yes, you'll need to override the class and then call setSharedImageManagerWithConfiguration: on the overridden class (since that method calls [self class] when instantiating the shared image manager, it'll correctly return your subclass when other places call PINRemoteImageManager.shared(). https://github.com/pinterest/PINRemoteImage/blob/18637e4414d77fbc95cd7d499179a9880e9a78d8/Source/Classes/include/PINRemoteImageManager.h#L219-L225

It's certainly not the most elegant solution and annoyingly has to be called before any parts of your code call the .shared() method, but it should work!

garrettmoon avatar Apr 28 '23 18:04 garrettmoon

Really appreciate the quick responses, thank you! I think this is enough for me to go on, I do think something like what I mentioned in the issue would be pretty slick in the future but this should work!

MilesV64 avatar Apr 28 '23 18:04 MilesV64