开发者

using PDFScroller with a navigationController

I am trying to use this PDFScroller code http://dl.dropbox.com/u/5391413/PDFScroller.zip (thanks jbm). I would like to display a pdf from a list (a tableview) thanks to a navigationController. I init a PhotoViewControler with a pdf file name and display it correctly. T开发者_高级运维he problem is that after I have loaded a file once, I don't manage to clean the pdfDoc ref and this causes a crash after coming back to the view list and loading another file.

I tried to release the pdfDoc ref, or set to nil in the PhotoViewController dealloc method but it does not work. One more thing: the viewDidUnload method of the PhotoViewController is not called when popping the viewController out of the navigationcontroller stack... is that normal?

thanks

G.


here is how I launch a PhotoViewController from he root viewController:

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 contains the name of the pdf file, I set a property in the PhotoViewController.Here is the PhotoViewController implementation (little different from the original sample code):

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

The problem is: in the - (CGPDFPageRef) pdfPage: (NSInteger) index method, __pdfDoc remains the same even after the PhotoViewController was popped off the navigationController stack, and this causes a crash. I don't where to clean it correctly.

thanks

Guillaume

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜