首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iPhone :实现Ole Leaves

iPhone :实现Ole Leaves
EN

Stack Overflow用户
提问于 2011-11-24 16:29:01
回答 1查看 423关注 0票数 1

嗨,我最近正在与PDF应用程序接口工作,我有这个问题,我需要显示一个Leaves文件,我的ViewController类有一个nib文件,我实现的代码基础的api示例代码,但没有显示任何东西!我是不是漏掉了什么?

代码语言:javascript
复制
#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];
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-11-24 16:45:42

你如何实例化你的控制器?

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

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

请注意,initWithNibName的完整签名为:

代码语言:javascript
复制
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle

您有更多的选项可用,比如通过调用init (而不是initWithNibName)来创建控制器,或者甚至创建一个方法setPDF,然后,在以任何您喜欢的方式创建控制器之后,您可以调用此方法,该方法依次调用initialize,等等……

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

代码语言:javascript
复制
- (void)loadView {
    [super loadView];
    if (leavesView) {
        leavesView.frame = self.view.bounds;
        leavesView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        [self.view addSubview:leavesView];
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8254053

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档