在应用中,我们有时想预览文件,但是又不想为此而专门写一个viewController。这时QLPreviewController的作用就来了。
QLPreviewController最简单的用法如下:
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *fileName = [path stringByAppendingPathComponent:@"test.jpg"];
NSURL *fileRUL = [NSURL fileURLWithPath:fileName];
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:fileRUL];
self.documentInteractionController.delegate = self;
[self.documentInteractionController presentPreviewAnimated:YES];
结果如下:
具体如何设定预览的内容和行为,是在QLPreviewControllerDelegate和QLPreviewControllerDataSource中完成的,所以这里只需要在controller中实现QLPreviewControllerDelegate和QLPreviewControllerDataSource相关方法即可
// Returns the number of items that the preview controller should preview - (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)previewController { NSInteger numToPreview = 0; NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow]; if (selectedIndexPath.section == 0) numToPreview = NUM_DOCS; else numToPreview = self.documentURLs.count; return numToPreview; } // returns the item that the preview controller should preview - (id)previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)idx { NSURL *fileURL = nil; NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow]; if (selectedIndexPath.section == 0) { fileURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:documents[idx] ofType:nil]]; } else { fileURL = [self.documentURLs objectAtIndex:idx]; } return fileURL; }
另外一些解决方案:
在SDK中打开其他接入应用的解决方案
http://blog.csdn.net/arthurchenjs/article/details/6920631
应用程序间通讯 - URL Scheme
http://blog.csdn.net/flower4wine/article/details/6454957
在Mac和iOS中注册自定义的URL Scheme
http://cocoa.venj.me/blog/custom-url-scheme-on-mac-and-ios/
iOS应用中打开一些其他应用的URL-Scheme
iPhone开发技巧 URL Scheme启动进程调试教程
http://mobile.51cto.com/iphone-278973.htm
应用之间调用
http://blog.csdn.net/pjk1129/article/details/6641211