您的位置:首页 > 产品设计 > UI/UE

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];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐