我正在尝试将一个大的Keynote文件(~150MB)加载到UIWebView
中,但我不断收到内存警告,我的应用程序崩溃。
有没有办法解决这个问题?
打开这么大的文件而不在其他应用程序中打开它们的正确方法是什么?
当您直接从 url 打开 UIWebView
中的文件时,下载的内容会临时存储在 RAM 中。RAM是整个设备的共享空间,必须执行其他与操作系统相关的任务。因此,由于内存压力和资源紧缩,您的应用程序被iOS杀死。
建议在后台直接将您的内容NSDocumentsDirectory
写入文件,稍后在UIWebView
加载文件。
据我所知,我可以向您提出以下建议。
下载部分
- JGDownloadAcceleration 一个多部分下载加速器库。
- TCBlobDownload 适用于 iOS 的竞争性大文件下载
预览部分
- CGPDF API 讨论
- 另一个讨论
希望有帮助。
如果它是一个大文件,你不能/不应该使用 UIWebView
.
为什么?我尝试显示一个包含几张图像的文档文件 (docx),但在抛出内存警告后,我的应用程序崩溃了。原因很简单。虽然文件大小为 ~2.5 MB,但设备没有足够的 RAM/内存来显示所有位图图像(嵌入在文档中)。使用 Instruments 调试问题显示,应用程序内存从 30 MB 激增到 230 MB。我想你正在经历类似的事情。
可能的解决方案:
-
不允许用户在其移动设备上打开大文件。要么,要么在收到内存警告时正常停止/停止
UIWebView
加载过程。- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; if ([self.webView isLoading]) { [self.webView stopLoading]; } }
-
请尝试改用
[UIApplication sharedApplication] openURL:]
方法。 -
请尝试改用
UIDocumentInteractionController
。UIDocumentInteractionController *documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:targetURL]; documentInteractionController.delegate = self; BOOL present = [documentInteractionController presentPreviewAnimated:YES]; if (!present) { // Allow user to open the file in external editor CGRect rect = CGRectMake(0.0, 0.0, self.view.frame.size.width, 10.0f); present = [documentInteractionController presentOpenInMenuFromRect:rect inView:self.view animated:YES]; if (!present) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Cannot preview or open the selected file" delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", nil) otherButtonTitles:nil, nil]; [alertView show]; } }
注意:我还没有尝试使用上述方法打开主题演讲文件。为了使用 UIDocumentInteractionController
,您必须先下载文件。
希望这有帮助。