ルート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は同じままであり、これによりクラッシュが発生します。私はそれを正しく掃除する場所がありません。
ありがとう
ギヨーム