Quicklook/QLPreviewController shows a blank page i

2019-01-31 14:51发布

I am trying to preview pdf file in QLPreviewController and using the below code. It works fine on iOS7 and for other type of files (JPG/PNG) on iOS8 as well but when I try to open pdf it shows blank page instead content on iOS8. Its weird that it still shows name of pdf in title view.

Code:

QLPreviewController *previewer = [[QLPreviewController alloc] init];
previewer.dataSource = self;
previewer.currentPreviewItemIndex = 0;
[self presentViewController:previewer animated:NO completion:nil];

And QLPreviewControllerDataSource methods:

- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
    return 1;
}

- (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
    return [NSURL fileURLWithPath:self.pdfUrl];
}

9条回答
Deceive 欺骗
2楼-- · 2019-01-31 15:17

I just recently encountered a blank page again and I have found the reason to be that I did something like

previewController.dataSource = [[MyDataSource alloc] initWithSomething];

Since the QLPreviewController holds its dataSource as a weak variable, the data source immediately vanishes.

Keeping a strong reference to the dataSource helped.

查看更多
劳资没心,怎么记你
3楼-- · 2019-01-31 15:21

For iOS 9.2: In Monotouch.ios,

use

var url = NSUrl.FromFilename(NSBundle.MainBundle.PathForResource ("pdf.pdf", null)); 

instead of

var url = NSUrl.FromFilename("pdf.pdf");

It will work for all files. (jpg,png,pdf,etc)

查看更多
来,给爷笑一个
4楼-- · 2019-01-31 15:22

Use this code if you in desperate need to use QLPreviewController embedded in UINavigationController:

#import <Quicklook/QLPreviewController.h>

...

QLPreviewController *pc = [[QLPreviewController alloc] init];
pc.delegate = self;
pc.dataSource = self;

UINavigationController *nc = [[UINavigationController alloc] init];
nc.view.backgroundColor = [UIColor whiteColor];
nc.navigationBarHidden = YES;

[self presentViewController:nc animated:YES completion:nil];
[nc pushViewController:pc animated:NO];
查看更多
登录 后发表回答