ios – 禁用UIWebView DiskImageCache

ios – 禁用UIWebView DiskImageCache,第1张

概述加载包含图像的文档(例如Microsoft Word docx文件)时,UIWebView将始终在收到内存警告时缓存图像,而不管缓存策略如何. 下面是一个示例代码段: NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:1024 * 1024 加载包含图像的文档(例如Microsoft Word docx文件)时,UIWebVIEw将始终在收到内存警告时缓存图像,而不管缓存策略如何.

下面是一个示例代码段:

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,打开文档中的所有图像都保存在此处.

在UIWebVIEw加载新请求后,如果我调用

[sharedCache removeAllCachedResponses];

这些临时图像被删除.

防止缓存图像的唯一方法是调用

[NSClassFromString(@"Webcache") performSelector:@selector(setDisabled:) withObject:[NSNumber numberWithBool:YES]];

但这意味着使用私有API.

是否有“Apple友好”的方式来实现这一目标?

解决方法 在查看WebKit“未记录的”偏好后,我想出了这个.通过以下设置,可以在应用程序的整个生命周期内禁用diskimageCache:

[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitdiskimageCacheEnabled"];[[NSUserDefaults standardUserDefaults] synchronize];
总结

以上是内存溢出为你收集整理的ios – 禁用UIWebView DiskImageCache全部内容,希望文章能够帮你解决ios – 禁用UIWebView DiskImageCache所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1065069.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存