Reputation: 59989
How would I load an NSImage
from URL without caching it? It should always be re-loaded from the remote source. But even if I am not connected to the internet, I end up with a valid image. Setting the cache mode to the already loaded image does not have any effect.
let url = NSURL(string: "http://thecybershadow.net/misc/stackoverflow.png")
let img = NSImage(byReferencingURL: url!)
img.cacheMode = NSImageCacheMode.Never
NSLog("Found image with size \(img.size)")
When I run this snippet in a playground, it actually never caches, even if I set NSImageCacheMode.Always
.
But when I run this snippet in my application, it always returns a valid image, even if I'm not connected to the internet.
I know I could simply use a cache buster parameter with a random number or timestamp. But this is a theoretical question and I'm interested in how to prevent caching in the first place.
Upvotes: 0
Views: 926
Reputation: 2631
Ran into this issue in objc as well (I think, trying out this fix now).
I assume you can work around it thusly:
NSString *imgURL = @"http://thecybershadow.net/misc/stackoverflow.png";
NSURL *u = [NSURL URLWithString:imgURL];
NSError *error;
NSData *d = [NSData dataWithContentsOfURL:u
options:NSDataReadingUncached
error:&error];
NSImage *image = [[NSImage alloc] initWithData:d];
I believe that will load from the network resource every time.
I might take a crack at swift after I do some testing to figure out if it actually works.
Upvotes: 1