iOS如何阅览和选择应用打开网络下载的pdf、word、jpg、MP4等文件

本文介绍如何使用UIDocumentInteractionController在iOS应用中实现文件预览功能,包括图片、文档及视频等不同类型的文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过创建UIDocumentInteractionController来实现这一功能,如下栗子:

#pragma mark - 展示文件

- (void)showDownloadFile:(NSString *)fileName{

  // 从沙盒中获取(以下路径是我从网络下载文件存放的路径)

  NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

  NSString *fileDir = [NSStringstringWithFormat:@"%@/Download/File", docPath];

  NSString *filePath = [fileDir stringByAppendingPathComponent:fileName]; //fileName如:text.jpg、text.pptx、text.mp4等

  UIDocumentInteractionController * docC = [self setupControllerWithURL:[NSURLfileURLWithPath:filePath]

                                                          usingDelegate:self];

//        [docC presentOpenInMenuFromRect:self.view.bounds inView:self.view animated:YES]; //这一方式没有阅览功能

    [docC presentPreviewAnimated:YES];

}

#pragma mark - 设置docController

- (UIDocumentInteractionController *)setupControllerWithURL: (NSURL *) fileURL

                                               usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate {

     UIDocumentInteractionController *interactionController = [UIDocumentInteractionControllerinteractionControllerWithURL: fileURL];

    interactionController.delegate = interactionDelegate; // 不要忘记遵守UIDocumentInteractionControllerDelegate协议

    return interactionController;

}

#pragma mark - 预览窗口的父窗口,不填写则无展示效果

- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{

    return self;  

}

#pragma mark - 代理方法

- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller{

    return self.view;

} 

- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller{

    return self.view.frame

}

 提供几个下载地址:

http://pic6.nipic.com/20100330/4592428_113348097000_2.jpg

http://lcell.bnu.edu.cn/upImages/resource/20110507140606.pptx

http://120.25.226.186:32812/resources/videos/minion_01.mp4

随笔随记,以免忘记

转载于:https://www.cnblogs.com/zlyunduan/p/5386078.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值