iOS - Opening a PDF via Quicklook without using UI

2019-06-10 18:50发布

问题:

I am trying to open a PDF via the QuickLook framework without using UIScrollView...

I believe I'm missing something...

Where I believe I'm going wrong is that I need to use a QLPreviewController and on the QLPreviewController is a dataSource that has to conform to QLPreviewItem. The documentation states that NSURL does conform to QLPriewItem so I'm setting the preview.dataSource to an NSURL which is throwing an error:

[NSURL numberOfPreviewItemsInPreviewController:]: unrecognized selector sent to instance

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL numberOfPreviewItemsInPreviewController:]: unrecognized selector sent to instance 0x5b5f200'

Which makes me think that NSURL does not conform.

all the code I think is necessary...

- (BOOL)previewController:(QLPreviewController *)controller shouldOpenURL:(NSURL *)url forPreviewItem:(id <QLPreviewItem>)item {

    return YES;
}

- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller {

    return [documents count];
}

- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index {

    return [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[documents objectAtIndex:index] ofType:nil]];
}

- (void)pushPDF {

    QLPreviewController *preview = [[QLPreviewController alloc] init];
    preview.dataSource = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"MCIT_Quiz" ofType:@"pdf"]];
    //preview.currentPreviewItemIndex = 0;
    [self presentModalViewController:preview animated:YES];
    [preview release];
}

回答1:

Well, I don't see where an NSURL conforms to QLPreviewControllerDataSource. I think you want

 preview.dataSource = self;

And then your already written routines (numberOfPreviewItemsInPreviewController and previewController) would return the appropriate NSURL (although it's not clear how "documents" gets filled.).



回答2:

I ended up just creating another class to hold my values and use as a datasource, a bit quick and dirty but it works.

//
//  documentList.h
//

#import <Foundation/Foundation.h>
#import <QuickLook/QuickLook.h>


@interface DocumentList : NSObject <QLPreviewControllerDataSource, QLPreviewControllerDelegate> {
    NSArray *documents;
}

@property (nonatomic, retain) NSArray *documents;

-(void)createList;
-(NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller;
- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index;

@end

inserting text to break up the files

//
//  documentList.m
//

#import "DocumentList.h"

@implementation DocumentList

@synthesize documents;

-(void) createList {

    documents = [[NSArray arrayWithObjects:@"Quiz.pdf", nil] retain];
}

-(NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller {

    return [documents count];
}

- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index {

return [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[documents objectAtIndex:index] ofType:nil]];
}

@end