iPhone : Implement Ole Leaves API

标签 iphone ios xcode ipad sdk

嗨,我最近在与 Leaves 合作api,我对此有疑问,我需要显示一个 PDF 文件,并且我的 ViewController 类有一个 nib 文件,我根据 API 示例代码实现了代码库,但没有显示任何内容!我错过了什么吗?

#import "BookViewController.h"
#import "Utilities.h"

@implementation BookViewController



/////EDITED//////////////


- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    if (self = [super initWithNibName:@"BookViewController" bundle:nil]) {

        CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("paper.pdf"), NULL, NULL);
        pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
        CFRelease(pdfURL);

    }
    return self;
}





- (void)dealloc {
    CGPDFDocumentRelease(pdf);
    [super dealloc];
}



- (void) displayPageNumber:(NSUInteger)pageNumber {
    self.navigationItem.title = [NSString stringWithFormat:
                                 @"Page %u of %u", 
                                 pageNumber, 
                                 CGPDFDocumentGetNumberOfPages(pdf)];
}



#pragma mark  LeavesViewDelegate methods

- (void) leavesView:(LeavesView *)leavesView willTurnToPageAtIndex:(NSUInteger)pageIndex {
    [self displayPageNumber:pageIndex + 1];
}



#pragma mark LeavesViewDataSource methods

- (NSUInteger) numberOfPagesInLeavesView:(LeavesView*)leavesView {
    return CGPDFDocumentGetNumberOfPages(pdf);
}

- (void) renderPageAtIndex:(NSUInteger)index inContext:(CGContextRef)ctx {
    CGPDFPageRef page = CGPDFDocumentGetPage(pdf, index + 1);
    CGAffineTransform transform = aspectFit(CGPDFPageGetBoxRect(page, kCGPDFMediaBox),
                                            CGContextGetClipBoundingBox(ctx));
    CGContextConcatCTM(ctx, transform);
    CGContextDrawPDFPage(ctx, page);
}


#pragma mark UIViewController

- (void) viewDidLoad {
    [super viewDidLoad];
    leavesView.backgroundRendering = YES;
    [self displayPageNumber:1];
}

最佳答案

你是如何实例化你的 Controller 的?

如果您调用 initWithNibName(正如我想的那样,因为您说它有一个 nib 文件),您将完全跳过本地 init 方法(即,一个在 BookController 中);因此,PDF 处理根本没有初始化。

尝试将您的 init 方法重命名为 initWithNibName,它应该可以工作;或者创建一个 initWithNibName 并让它调用你 init(谷歌搜索“指定构造函数”以了解有关此模式的更多信息)。

注意 initWithNibName 的完整签名是:

- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle

您有更多选择,例如通过调用 init(而不是 initWithNibName)创建 Controller ,甚至创建方法 setPDF,然后, 在您以任何您喜欢的方式创建 Controller 之后,您可以调用此方法,该方法依次调用 initialize 等...

编辑:如果您决定手动实例化 Controller ,请不要忘记定义loadView:

- (void)loadView {
    [super loadView];
    if (leavesView) {
        leavesView.frame = self.view.bounds;
        leavesView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        [self.view addSubview:leavesView];
    }
}

关于iPhone : Implement Ole Leaves API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8254053/

相关文章:

ios - 快速统计登录次数

iphone - 谷歌地图 View 如何在 map 缩放时获取原始点或注释框?

iphone - 工具栏上带有自定义按钮的 MPMoviePlayerController

ios - 如何在 ios 中为 UIScrollView 设置对齐方式

项目重命名后 iPhone 应用程序图标显示为空白

ios - 在后台收集加速度计数据

xcode - 是否可以在 Snow Leopard 上获取适用于 Xcode 4.2 的 iOS 5.1 SDK?

xcode - 如何将文件添加到 Xcode 项目? (编写Unity插件)

iphone - 在之前可以使用 3.0.1 的 iPhone 上配置 iPhone 时出现问题

ios - 从 AppDelegate 调用方法 - Objective-C