iOS利用UIDocumentInteractionController和Quick Look打开或预览文档
2015-05-22 11:41
507 查看
在App的开发过程中,我们避免不了要打开软件中的文件,例如:Excel文件,Word文件,图片文件等不同格式的文件或者想要通过第三方的App来打开这些文件,那么我们就要用到UIDocumentInteractionController和Quick Look来解决这些问题了。
UIDocumentInteractionController的使用方法UIDocumentInterRactionController使用时的注意事项
Quick Look的使用方法
一、UIDocumentInteractionController的使用方法
- 首先创建一个UIDocumentInteractionController对象,并对该对象初始化一个URL作为文件路径
1、首先要遵循UIDocumentInteractionControllerDelegate 2、其次是创建一个UIDocumentInteractionController对象 @property(nonatomic,retain)UIDocumentInteractionController *docController; 3、在方法中进行UIDocumentInteractionController对象的初始化 - (void)open{ NSString *fileName = [self.listDicobjectForKey:@"fileName"]; NSString* path = [NSHomeDirectory()stringByAppendingPathComponent: _docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:path]];//为该对象初始化一个加载路径 _docController.delegate =self;//设置代理 //直接显示预览 // [_docController presentPreviewAnimated:YES]; CGRect navRect =self.navigationController.navigationBar.frame; navRect.size =CGSizeMake(1500.0f,40.0f); //显示包含预览的菜单项 [_docController presentOptionsMenuFromRect:navRectinView:self.viewanimated:YES]; //显示不包含预览菜单项 //[docController presentOpenInMenuFromRect:navRect inView:self.view animated:YES]; } 4、代理方法 - (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller { return self; } - (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller { return self.view; } - (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller { return self.view.frame; }
二、UIDocumentInterRactionController使用时的注意事项
1、UIDocumentInterRactionController定义的时候一定要是retain类型的,因为必须要对该对象进行持有;
2、当选择显示包含预览的菜单项
[_docController presentOptionsMenuFromRect:navRectinView:self.viewanimated:YES];时应该注意一点,该方法会触发该类的内置打印输出,会将日志信息打印出来,从而导致App崩溃严重的可能会导致手机死机,现在还未找到解决方法。类似输出结果如下(此处省略该输出的后面部分内容,因为太多了):
unknown activity items supplied: ( { "public.jpeg" = <ffd8ffe0 00104a46 49460001 01010060 00600000 ffdb0043 00030202 03020203 03030304 03030405 08050504 04050a07 ......
当上述方法确实无法满足你的要求的时候就可以考虑一下用Quick Look了。
三、Quick Look的使用方法
1、首先要添加QuickLook.FrameWork框架,具体怎么添加的我就不解释了。
2、在需要用到的Controller中添加头文件#import
- (void)open{ QLPreviewController *myQlPreViewController = [[QLPreviewController alloc]init]; myQlPreViewController.delegate =self; myQlPreViewController.dataSource =self; [myQlPreViewController setCurrentPreviewItemIndex:0]; [self presentViewController:myQlPreViewControlleranimated:YEScompletion:nil];
5、代理方法
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller { return 1; } - (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index { NSString *fileName = [self.listDicobjectForKey:@"fileName"]; NSString* path = [NSHomeDirectory()stringByAppendingPathComponent:[NSStringstringWithFormat:@"Documents/%@",fileName]]; return [NSURLfileURLWithPath:path]; }
相关文章推荐
- iOS利用UIDocumentInteractionController和Quick Look打开或预览文档
- 【iOS功能实现】之利用UIDocumentInteractionController打开和预览文档
- 【iOS功能实现】之利用UIDocumentInteractionController打开和预览文档
- iOS 利用UIDocumentInteractionController打开和预览文档
- 之利用UIDocumentInteractionController打开和预览文档
- UIDocumentInteractionController -iOS预览、打开文档(doc、pdf等)
- UIDocumentInteractionController的使用之打开和预览文档
- UIDocumentInteractionController 预览和打开文档
- iOS Document Interaction(预览和打开文档) 编程指南
- iOS 调起第三方程序打开文件 ( UIDocumentInteractionController )
- iOS 两种预览使用详解(配Demo下载)( Quicklook& UIDocumentInteractionController)
- (浏览文档)UIDocumentInteractionController打开自己的第三方应用、使用QLPreviewController浏览文档
- iOS UIDocumentInteractionController 设置第三方打开方式的 参数详解
- iOS 第三方应用中打开自己的文件(UIDocumentInteractionController)
- iOS UIDocumentInteractionController 第三方打开详解
- 使用第三方APP打开本地文档 (UIDocumentInteractionController)
- UIDocumentInteractionController 第三方应用中打开自己的文件 方法
- ios中通过UIDocumentInteractionControllers调用第三方应用打开本地压缩文件
- UIDocumentInteractionController 文件预览
- ios开发之--UIDocumentInteractionController的使用(实现更多分享服务)