ios 預覽文件-QLPreviewController用法


在應用中,我們有時想預覽文件,但是又不想為此而專門寫一個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

http://willonboy.tk/?p=742

iPhone開發技巧 URL Scheme啟動進程調試教程

http://mobile.51cto.com/iphone-278973.htm

應用之間調用

http://blog.csdn.net/pjk1129/article/details/6641211

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM