iOS - 通过 Quicklook 打开 PDF,而不使用 UIScrollView

发布于 2024-11-03 17:30:29 字数 1419 浏览 1 评论 0原文

我正在尝试通过 QuickLook 框架打开 PDF,而不使用 UIScrollView...

我相信我错过了一些东西...

我认为我出错的地方是我需要使用 QLPreviewController 并且 QLPreviewController 上是一个数据源必须符合 QLPreviewItem。该文档指出 NSURL 确实符合 QLPriewItem,因此我将 Preview.dataSource 设置为引发错误的 NSURL:

[NSURL numberOfPreviewItemsInPreviewController:]: 无法识别的选择器发送到实例

终止应用程序由于未捕获异常'NSInvalidArgumentException',原因:'-[NSURL numberOfPreviewItemsInPreviewController:]:无法识别的选择器发送到实例 0x5b5f200'

这让我认为 NSURL 不符合要求。

我认为所有的代码都是必要的......

- (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];
}

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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哽咽笑 2024-11-10 17:30:29

我最终只是创建了另一个类来保存我的值并用作数据源,有点快速和肮脏,但它有效。

//
//  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

插入文本来分解文件

//
//  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

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
爱要勇敢去追 2024-11-10 17:30:29

好吧,我没有看到 NSURL 哪里符合 QLPreviewControllerDataSource。我想你想要

 preview.dataSource = self;

然后你已经编写的例程(numberOfPreviewItemsInPreviewController和previewController)将返回适当的NSURL(尽管不清楚“文档”是如何填充的。)。

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.).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文