您好,登錄后才能下訂單哦!
在應用開發中,有時候需要預覽文檔和視頻,使用 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 就能回到主界面。
原文地址:https://www.exchen.net/ios-%E6%96%87%E4%BB%B6%E9%A2%84%E8%A7%88-uidocumentinteractioncontroller.html
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。