使用带有navigationController的PDFScroller

时间:2011-02-10 02:22:41

标签: iphone pdf scroller

我正在尝试使用此PDFScroller代码http://dl.dropbox.com/u/5391413/PDFScroller.zip(感谢jbm)。由于navigationController,我想从列表(tableview)中显示pdf。我使用pdf文件名初始化PhotoViewControler并正确显示它。问题是,在我加载一次文件之后,我无法清理pdfDoc ref,这会在返回视图列表并加载另一个文件后导致崩溃。

我试图释放pdfDoc ref,或者在PhotoViewController dealloc方法中设置为nil,但它不起作用。 还有一件事:当将viewController弹出导航控制器堆栈时,没有调用PhotoViewController的viewDidUnload方法......这是正常的吗?

感谢

-G。

1 个答案:

答案 0 :(得分:0)

这是我如何从他的根viewController:

启动PhotoViewController
PhotoViewController *detailViewController = [[PhotoViewController alloc] initWithNibName:@"PhotoViewController" bundle:nil pdfName:tmpName];
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];

tmpName包含pdf文件的名称,我在PhotoViewController中设置了一个属性。这是PhotoViewController实现(与原始示例代码略有不同):

- (void)loadView 
{    
    // Step 1: make the outer paging scroll view
    CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
    pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
    pagingScrollView.pagingEnabled = YES;
    pagingScrollView.backgroundColor = [UIColor blackColor];
    pagingScrollView.showsVerticalScrollIndicator = NO;
    pagingScrollView.showsHorizontalScrollIndicator = NO;
    pagingScrollView.contentSize = CGSizeMake(pagingScrollViewFrame.size.width * [self pdfPageCount],
                                              pagingScrollViewFrame.size.height);
    pagingScrollView.delegate = self;
    self.view = pagingScrollView;


    // Step 2: prepare to tile content
    recycledPages = [[NSMutableSet alloc] init];
    visiblePages  = [[NSMutableSet alloc] init];
    [self tilePages];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    [pdfName release];
    pdfName = nil;
    [pagingScrollView release];
    pagingScrollView = nil;
    //CGPDFDocumentRelease(__pdfDoc);
    //__pdfDoc = nil;
    [recycledPages release];
    recycledPages = nil;
    [visiblePages release];
    visiblePages = nil;
}

- (void)dealloc
{
    NSLog(@"dealloc");
    [pdfName release];
    [pagingScrollView release];
    [super dealloc];
}

- (void)tilePages 
{
    // Calculate which pages are visible
    CGRect visibleBounds = pagingScrollView.bounds;
    int firstNeededPageIndex = floorf(CGRectGetMinX(visibleBounds) / CGRectGetWidth(visibleBounds));
    int lastNeededPageIndex  = floorf((CGRectGetMaxX(visibleBounds)-1) / CGRectGetWidth(visibleBounds));
    firstNeededPageIndex = MAX(firstNeededPageIndex, 0);
    lastNeededPageIndex  = MIN(lastNeededPageIndex, [self pdfPageCount] - 1);

    // Recycle no-longer-visible pages 
    for (ImageScrollView *page in visiblePages) {
        if (page.index < firstNeededPageIndex || page.index > lastNeededPageIndex) {
            [recycledPages addObject:page];
            [page removeFromSuperview];
        }
    }
    [visiblePages minusSet:recycledPages];

    // add missing pages
    for (int index = firstNeededPageIndex; index <= lastNeededPageIndex; index++) {
        if (![self isDisplayingPageForIndex:index]) {
            ImageScrollView *page = [self dequeueRecycledPage];
            if (page == nil) {
                page = [[[ImageScrollView alloc] init] autorelease];
            }
            [self configurePage:page forIndex:index];
            [pagingScrollView addSubview:page];
            [visiblePages addObject:page];
        }
    }
}

- (void)configurePage:(ImageScrollView *)page forIndex:(NSUInteger)index
{
    page.index = index;
    page.frame = [self frameForPageAtIndex:index];

    // Use tiled images
    [page displayTiledImageNamed: [self pdfPage: index]
                            size: [self pdfSize: index]];
}

static CGPDFDocumentRef __pdfDoc = nil;

- (CGPDFPageRef) pdfPage: (NSInteger) index {
    if( ! __pdfDoc ) {
         NSString *pdfPath = [[NSBundle mainBundle] pathForResource: pdfName ofType:nil];
         CFURLRef url = CFURLCreateWithFileSystemPath( NULL, (CFStringRef)pdfPath,
         kCFURLPOSIXPathStyle, NO );
         __pdfDoc = CGPDFDocumentCreateWithURL( url );

    }
    if( __pdfDoc ) {
        size_t pdfPageCount = CGPDFDocumentGetNumberOfPages( __pdfDoc );
        index++; // incoming param is zero-based, CGPDF calls are 1-based
        if( index < 1 )
            index = 1;
        if( index > pdfPageCount )
            index = pdfPageCount;
        CGPDFPageRef page = CGPDFDocumentGetPage( __pdfDoc, index );
        return page;
    }
    return nil;
}

-(IBAction)backToListView{
    [self.navigationController popViewControllerAnimated:YES];
}

问题是:在- (CGPDFPageRef) pdfPage: (NSInteger) index方法中,即使在从NavigationController堆栈弹出PhotoViewController后,__ pdfDoc仍保持不变,这会导致崩溃。我无法正确清理它。

感谢

纪尧姆