這裏是我從他的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
方法中,即使PhotoViewController從navigationController堆棧彈出,__pdfDoc仍保持不變,這導致崩潰。我沒有正確清理它的地方。
感謝
紀堯姆
後一些代碼,請 – 2011-02-10 02:28:33