iOS开发-QLPreviewController与UIDocumentInteractionController显示文档

iOS开发-QLPreviewController与UIDocumentInteractionController显示文档

在应用中,我们有时想预览文件, 可以使用QLPreviewController与UIDocumentInteractionController

一、QLPreviewController与UIDocumentInteractionController

QLPreviewController是一个 UIViewController ,用于管理预览项的用户体验。

UIDocumentInteractionController

官方文档:UIDocumentInteractionController 一种视图控制器,用于预览、打开或打印应用程序无法直接处理的文件格式的文件。名字叫控制器,但却是继承自 NSObject。

二、实现预览显示文档

使用QLPreviewController与UIDocumentInteractionController预览显示文档

#import "INDocumentPreviewViewController.h"
#import <QuickLook/QuickLook.h>

@interface INDocumentPreviewViewController ()<QLPreviewControllerDataSource, UIDocumentInteractionControllerDelegate>

@property (strong, nonatomic) QLPreviewController *previewController;
@property (copy, nonatomic) NSURL *fileURL; //文件路径
@property(nonatomic,strong) UIDocumentInteractionController * documentVC;

@end

@implementation INDocumentPreviewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *shareBtn;
    shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    shareBtn.frame = CGRectMake(50, 100, 200, 46);
    shareBtn.layer.cornerRadius = 4;
    shareBtn.backgroundColor = [UIColor brownColor];
    [shareBtn setTitle:@"QLPreviewController" forState:UIControlStateNormal];
    [shareBtn addTarget:self action:@selector(showQLPreview) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:shareBtn];
    
    UIButton *shareBtn2;
    shareBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    shareBtn2.frame = CGRectMake(50, 200, 200, 46);
    shareBtn2.layer.cornerRadius = 4;
    shareBtn2.backgroundColor = [UIColor brownColor];
    [shareBtn2 setTitle:@"UIDocumentInteraction" forState:UIControlStateNormal];
    [shareBtn2 addTarget:self action:@selector(documentInteraction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:shareBtn2];
    
    self.previewController  =  [[QLPreviewController alloc]  init];
    self.previewController.dataSource  = self;
}

- (void)showQLPreview {
    self.fileURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"七牛实时音视频云白皮书.pdf" ofType:nil]];
    [self presentViewController:self.previewController animated:YES completion:nil];
    //刷新界面,如果不刷新的话,不重新走一遍代理方法,返回的url还是上一次的url
    [self.previewController refreshCurrentPreviewItem];
}


- (void)documentInteraction {
    self.fileURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"七牛实时音视频云白皮书.pdf" ofType:nil]];
    NSURL *url = self.fileURL;

    self.documentVC = [UIDocumentInteractionController interactionControllerWithURL:url];
    self.documentVC.delegate = self;

    dispatch_async(dispatch_get_main_queue(), ^{
        BOOL b = [self.documentVC presentPreviewAnimated:YES];
    });
}

#pragma mark - QLPreviewControllerDataSource
-(id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
    return self.fileURL;
}

- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)previewController{
    return 1;
}

#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, self.view.frame.size.height);
}

@end

三、小结

iOS开发-QLPreviewController与UIDocumentInteractionController显示文档。

学习记录,每天不停进步。

相关推荐
服装学院的IT男2 小时前
【Android 13源码分析】Activity生命周期之onCreate,onStart,onResume-2
android
Arms2062 小时前
android 全面屏最底部栏沉浸式
android
服装学院的IT男2 小时前
【Android 源码分析】Activity生命周期之onStop-1
android
ChinaDragonDreamer5 小时前
Kotlin:2.0.20 的新特性
android·开发语言·kotlin
网络研究院7 小时前
Android 安卓内存安全漏洞数量大幅下降的原因
android·安全·编程·安卓·内存·漏洞·技术
凉亭下7 小时前
android navigation 用法详细使用
android
早起的年轻人10 小时前
Flutter String 按 ,。分割
flutter
小比卡丘10 小时前
C语言进阶版第17课—自定义类型:联合和枚举
android·java·c语言
前行的小黑炭11 小时前
一篇搞定Android 实现扫码支付:如何对接海外的第三方支付;项目中的真实经验分享;如何高效对接,高效开发
android
落落落sss12 小时前
MybatisPlus
android·java·开发语言·spring·tomcat·rabbitmq·mybatis