0

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

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

谢谢

G。

4

1 回答 1

0

这是我从根视图控制器启动 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方法中,即使 PhotoViewController 从 navigationController 堆栈中弹出,__pdfDoc 仍然保持不变,这会导致崩溃。我不知道在哪里正确清洁它。

谢谢

纪尧姆

于 2011-02-10T03:18:17.483 回答