我正在使用以下代码缓存图像-来自Apple Doc
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSString *url_string = [NSString stringWithFormat:@"%@",[rowData valueForKey:@"thumbnail_media_id"]];
NSURL *url = [NSURL URLWithString:url_string];
NSData * imageData = [NSData dataWithContentsOfURL:url];
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *image = [UIImage imageWithData:imageData];
// add image in cache
[[WKInterfaceDevice currentDevice] addCachedImage:image name:url_string];
[elementRow.rowImage setImage:image];
});
});
当从缓存中获取图像时,检查图像名称中是否包含
[[WKInterfaceDevice currentDevice] cachedImages];
然后使用方法获取图像-
[elementRow.rowImage setImageNamed:url_string];
但是无法获取缓存的图像,有什么帮助吗?
最佳答案
您可以使用NSCache
缓存图像,但是您可能不应该使用dataWithContentsOfURL。 NSURLSession具有内置的有用的缓存。
http://nshipster.com/nsurlcache/
关于ios - 如何在 watch 应用程序中缓存图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29544841/