禁用UIWebView DiskImageCache [英] Disable UIWebView DiskImageCache
问题描述
当加载包含图像的文档(例如 Microsoft Word docx 文件)时,UIWebView会在收到内存警告时始终缓存图像,而不考虑缓存策略。
When loading documents that contain images (such as a Microsoft Word docx file), the UIWebView will always cache the images when it receives a memory warning regardless of the cache policy.
下面是一个示例代码段:
Following, there's a sample code snippet:
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:1024 * 1024
diskCapacity:0
diskPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"URLCache"]];
[NSURLCache setSharedURLCache:sharedCache];
NSURLRequest* req = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"http://www.its.swinburne.edu.au/about/projects/templates/TechnicalSpecificationTemplatev1.1-[ProjectName]-[ver]-[YYYYMMDD].docx"]
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:10];
在这种情况下,如果发生内存警告,则会在 tmp中创建一个新文件夹应用程序的目录,通常名为 DiskImageCache-random_suffix ,打开的文档中的所有映像都保存在此处。
Under these circumstances, if a memory warning occurs, a new folder is created in the tmp directory of the app, usually named DiskImageCache-random_suffix and all the images in the opened document are saved here.
UIWebView 加载新请求,如果我打电话
After the UIWebView loads a new request, if I call
[sharedCache removeAllCachedResponses];
这些临时图像已删除。
防止缓存图像的唯一方法是调用
The only way to prevent caching the images is to call
[NSClassFromString(@"WebCache") performSelector:@selector(setDisabled:) withObject:[NSNumber numberWithBool:YES]];
但这意味着使用私有API。
but this means using private API.
是否有 Apple友好方式来实现这一目标?
Is there an "Apple friendly" way to achieve this ?
推荐答案
我在研究了WebKit中未记录后才发现了这一点。首选项。使用以下设置,可以在应用程序的整个生命周期中禁用DiskImageCache:
I figured it out after looking into the WebKit "undocumented" preferences. With the following setting, it is possible to disable DiskImageCache for the entire lifetime of the application:
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDiskImageCacheEnabled"];
[[NSUserDefaults standardUserDefaults] synchronize];
这篇关于禁用UIWebView DiskImageCache的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!