iOS 文件预览 UIDocumentInteractionController

发布时间:2020-07-22 11:19:09 作者:xcmer2005
来源:网络 阅读:905

在应用开发中,有时候需要预览文档和视频,使用 UIDocumentInteractionController 来预览文件非常方便,支持的格式比较多,比如 docx、xlsx、pdf、mov、mp4、jpg、png 等等都可以。具体代码如下:

@interface ViewController () <UIDocumentInteractionControllerDelegate>
 
@property(nonatomic,strong) UIDocumentInteractionController * documentVC;
 
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"第 7 章 Mach-O 文件格式解析" ofType:@"docx"];
    NSURL *url = [NSURL fileURLWithPath:path];
    
    self.documentVC = [UIDocumentInteractionController interactionControllerWithURL:url];
    self.documentVC.delegate = self;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        BOOL b = [self.documentVC presentPreviewAnimated:YES];
    });
}
 
#pragma mark 代理方法
//为快速预览指定控制器
- (UIViewController*)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController*)controller
{
    NSLog(@"%@",NSStringFromSelector(_cmd));
    return self;
}
 
//为快速预览指定View
- (UIView*)documentInteractionControllerViewForPreview:(UIDocumentInteractionController*)controller
{
    NSLog(@"%@",NSStringFromSelector(_cmd));
    return self.view;
}
 
//为快速预览指定显示范围
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController*)controller
{
    NSLog(@"%@",NSStringFromSelector(_cmd));
    //    return self.view.frame;
    return CGRectMake(0, 0, self.view.frame.size.width, 300);
}
 
@end

效果如下图,点击 Done 就能回到主界面。

iOS 文件预览 UIDocumentInteractionController


原文地址:https://www.exchen.net/ios-%E6%96%87%E4%BB%B6%E9%A2%84%E8%A7%88-uidocumentinteractioncontroller.html

推荐阅读:
  1. 如何把图片打包成文件
  2. 怎么打开在手机中的7z后缀文件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

文件 预览 roller

上一篇:使用linux的xauth命令修改x服务器访问授权

下一篇:如何去掉php数组的键名

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》