【发布时间】:2013-05-07 15:57:19
【问题描述】:
当加载包含图像的文档(例如 Microsoft Word docx 文件)时,无论缓存策略如何,UIWebView 都会在收到内存警告时缓存图像。
下面是示例代码sn-p:
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];
这种情况下,如果出现内存警告,会在app的tmp目录下新建一个文件夹,通常命名为DiskImageCache-random_suffix,所有图片都在打开的文档保存在这里。
UIWebView 加载新请求后,如果我调用
[sharedCache removeAllCachedResponses];
这些临时图像已被删除。
防止缓存图像的唯一方法是调用
[NSClassFromString(@"WebCache") performSelector:@selector(setDisabled:) withObject:[NSNumber numberWithBool:YES]];
但这意味着使用私有 API。
是否有“Apple 友好”的方式来实现这一点?
【问题讨论】:
标签: ios objective-c caching uiwebview docx