通过创建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
随笔随记,以免忘记