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显示文档。

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

相关推荐
沐怡旸23 分钟前
【底层机制】【Android】深入理解UI体系与绘制机制
android·面试
啊森要自信25 分钟前
【GUI自动化测试】YAML 配置文件应用:从语法解析到 Python 读写
android·python·缓存·pytest·pip·dash
下位子2 小时前
『AI 编程』用 Codex 开发识字小帮手应用
android·openai·ai编程
Zender Han2 小时前
Flutter 实现人脸检测 — 使用 google_mlkit_face_detection
android·flutter·ios
西西学代码2 小时前
Flutter---默认程序(计数器)
flutter
君逸臣劳2 小时前
玩Android Flutter版本,通过项目了解Flutter项目快速搭建开发
android·flutter
叫我龙翔3 小时前
【MySQL】从零开始了解数据库开发 --- 基本查询
android·mysql·数据库开发
2501_916008893 小时前
iOS 26 性能分析深度指南 包含帧率、渲染、资源瓶颈与 KeyMob 协助策略
android·macos·ios·小程序·uni-app·cocoa·iphone
撩得Android一次心动5 小时前
Android adb 基础使用指南
android·adb
为java加瓦5 小时前
PHP MQTT 订阅服务:实时消息接收与数据库存储解决方案
android