diff --git a/Classes/PDFContainer.m b/Classes/PDFContainer.m index eedafd32..ee7caa73 100644 --- a/Classes/PDFContainer.m +++ b/Classes/PDFContainer.m @@ -36,29 +36,29 @@ - (void)dealloc { CGPDFDocumentRelease(_PDFDocRef); [_fileURL release]; [_password release]; - + [super dealloc]; } - (BOOL)changeFileURL:(NSURL *)fileURL password:(NSString *)password { BOOL status = NO; - + if (fileURL != nil) // Check for non-nil file URL - { + { if (_fileURL) - [_fileURL release]; - + [_fileURL release]; + if (_password) - [_password release]; - + [_password release]; + _fileURL = [fileURL copy]; // Keep a copy _password = [password copy]; // Ditto CGPDFDocumentRelease(_PDFDocRef); - + _PDFDocRef = CGPDFDocumentCreateX((CFURLRef)fileURL, password); - + if (_PDFDocRef != NULL) // Check for non-NULL CGPDFDocRef { pages = CGPDFDocumentGetNumberOfPages(_PDFDocRef); // Set the total page count @@ -72,7 +72,7 @@ - (BOOL)changeFileURL:(NSURL *)fileURL password:(NSString *)password { NSAssert(NO, @"fileURL == nil"); } - + return status; } @@ -81,7 +81,7 @@ - (CGPDFPageRef)getPage:(NSInteger)pageNumber { if (pageNumber > pages) pageNumber = pages; // Check the upper page bounds CGPDFPageRef newPDFPageRef = CGPDFDocumentGetPage(_PDFDocRef, pageNumber); - + if (newPDFPageRef == NULL) // Check for non-NULL CGPDFPageRef { NSAssert(NO, @"CGPDFPageRef == NULL"); diff --git a/Classes/PDFViewTiled.m b/Classes/PDFViewTiled.m index b505d2d7..2b64dd4b 100644 --- a/Classes/PDFViewTiled.m +++ b/Classes/PDFViewTiled.m @@ -44,7 +44,7 @@ + (Class)layerClass - (id)initWithPage:(NSInteger)onPage frame:(CGRect)frame { if (self = [self initWithFrame:frame]) - { + { page = onPage; _PDFPageRef = [[PDFContainer sharedPDF] getPage:onPage]; @@ -56,11 +56,11 @@ - (id)initWithPage:(NSInteger)onPage frame:(CGRect)frame { { NSAssert(NO, @"CGPDFPageRef == NULL"); } - + } - + self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; - + return self; } @@ -72,7 +72,7 @@ - (void)willRotate } - (void)didRotate -{ +{ [self.layer setNeedsDisplay]; self.layer.hidden = NO; diff --git a/Classes/ReaderViewController.m b/Classes/ReaderViewController.m index 0bd41c26..769d839f 100644 --- a/Classes/ReaderViewController.m +++ b/Classes/ReaderViewController.m @@ -125,7 +125,7 @@ - (void)viewDidLoad [[PDFContainer sharedPDF] changeFileURL:fileURL password:nil]; [self initPDFScroll]; - + theToolbar = [UIToolbar new]; // Create the application toolbar theToolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth; @@ -252,7 +252,7 @@ - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrie NSLog(@"ReaderViewController.m -willRotateToInterfaceOrientation: [%d]", toInterfaceOrientation); NSLog(@" -> self.view.bounds = %@", NSStringFromCGRect(self.view.bounds)); #endif - + [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; } @@ -274,7 +274,7 @@ - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceO #endif //if (fromInterfaceOrientation == self.interfaceOrientation) return; // You get this when presented modally - + [super didRotateFromInterfaceOrientation:fromInterfaceOrientation]; } diff --git a/PDFScrollView.h b/PDFScrollView.h index 31df10b6..026a61e1 100644 --- a/PDFScrollView.h +++ b/PDFScrollView.h @@ -2,14 +2,14 @@ File: PDFScrollView.h Abstract: Centers image within the scroll view and configures image sizing and display. Version: 1.1 - + Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc. ("Apple") in consideration of your agreement to the following terms, and your use, installation, modification or redistribution of this Apple software constitutes acceptance of these terms. If you do not agree with these terms, please do not use, install, modify or redistribute this Apple software. - + In consideration of your agreement to abide by the following terms, and subject to these terms, Apple grants you a personal, non-exclusive license, under Apple's copyrights in this original Apple software (the @@ -25,13 +25,13 @@ implied, are granted by Apple herein, including but not limited to any patent rights that may be infringed by your derivative works or by other works in which the Apple Software may be incorporated. - + The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. - + IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS @@ -40,9 +40,9 @@ AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - + Copyright (C) 2010 Apple Inc. All Rights Reserved. - + */ #import diff --git a/PDFScrollView.m b/PDFScrollView.m index e6a9f101..bcba027b 100644 --- a/PDFScrollView.m +++ b/PDFScrollView.m @@ -2,14 +2,14 @@ File: PDFScrollView.m Abstract: Centers image within the scroll view and configures image sizing and display. Version: 1.1 - + Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc. ("Apple") in consideration of your agreement to the following terms, and your use, installation, modification or redistribution of this Apple software constitutes acceptance of these terms. If you do not agree with these terms, please do not use, install, modify or redistribute this Apple software. - + In consideration of your agreement to abide by the following terms, and subject to these terms, Apple grants you a personal, non-exclusive license, under Apple's copyrights in this original Apple software (the @@ -25,13 +25,13 @@ implied, are granted by Apple herein, including but not limited to any patent rights that may be infringed by your derivative works or by other works in which the Apple Software may be incorporated. - + The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. - + IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS @@ -40,9 +40,9 @@ OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - + Copyright (C) 2010 Apple Inc. All Rights Reserved. - + */ #import "PDFScrollView.h" @@ -67,7 +67,7 @@ - (id)initWithFrame:(CGRect)frame self.showsHorizontalScrollIndicator = NO; self.bouncesZoom = YES; self.decelerationRate = UIScrollViewDecelerationRateFast; - self.delegate = self; + self.delegate = self; [self setMaxMinZoomScalesForCurrentBounds]; self.contentSize = CGSizeMake(pdfView.bounds.size.width * 5, pdfView.bounds.size.height * 5); self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; @@ -89,29 +89,29 @@ - (void)dealloc #pragma mark - #pragma mark Override layoutSubviews to center content -- (void)layoutSubviews +- (void)layoutSubviews { [super layoutSubviews]; - + // center the image as it becomes smaller than the size of the screen - + CGSize boundsSize = self.bounds.size; CGRect frameToCenter = pdfView.frame; - + // center horizontally if (frameToCenter.size.width < boundsSize.width) frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2; else frameToCenter.origin.x = 0; - + // center vertically if (frameToCenter.size.height < boundsSize.height) frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2; else frameToCenter.origin.y = 0; - + pdfView.frame = frameToCenter; - + // if ([pdfView isKindOfClass:[PDFViewTiled class]]) { // // to handle the interaction between CATiledLayer and high resolution screens, we need to manually set the // // tiling view's contentScaleFactor to 1.0. (If we omitted this, it would be 2.0 on high resolution screens, @@ -147,24 +147,24 @@ - (void)setMaxMinZoomScalesForCurrentBounds { CGSize boundsSize = self.bounds.size; CGSize imageSize = CGSizeMake(pdfView.bounds.size.width * 5, pdfView.bounds.size.height * 5); - + // calculate min/max zoomscale CGFloat xScale = boundsSize.width / imageSize.width; // the scale needed to perfectly fit the image width-wise CGFloat yScale = boundsSize.height / imageSize.height; // the scale needed to perfectly fit the image height-wise CGFloat minScale = MIN(xScale, yScale); // use minimum of these to allow the image to become fully visible - + // on high resolution screens we have double the pixel density, so we will be seeing every pixel if we limit the // maximum zoom scale to 0.5. CGFloat maxScale = 5.0 / [[UIScreen mainScreen] scale]; - - // don't let minScale exceed maxScale. (If the image is smaller than the screen, we don't want to force it to be zoomed.) + + // don't let minScale exceed maxScale. (If the image is smaller than the screen, we don't want to force it to be zoomed.) if (minScale > maxScale) { minScale = maxScale; } - + // if (minScale < -5) // minScale = -5; - + self.maximumZoomScale = 5.0; //maxScale; self.minimumZoomScale = 1.0; //minScale; } @@ -172,23 +172,23 @@ - (void)setMaxMinZoomScalesForCurrentBounds #pragma mark - #pragma mark Methods called during rotation to preserve the zoomScale and the visible portion of the image -// returns the center point, in image coordinate space, to try to restore after rotation. +// returns the center point, in image coordinate space, to try to restore after rotation. - (CGPoint)pointToCenterAfterRotation { CGPoint boundsCenter = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds)); return [self convertPoint:boundsCenter toView:pdfView]; } -// returns the zoom scale to attempt to restore after rotation. +// returns the zoom scale to attempt to restore after rotation. - (CGFloat)scaleToRestoreAfterRotation { CGFloat contentScale = self.zoomScale; - + // If we're at the minimum zoom scale, preserve that by returning 0, which will be converted to the minimum // allowable scale when the scale is restored. if (contentScale <= self.minimumZoomScale + FLT_EPSILON) contentScale = 0; - + return contentScale; } @@ -206,17 +206,17 @@ - (CGPoint)minimumContentOffset // Adjusts content offset and scale to try to preserve the old zoomscale and center. - (void)restoreCenterPoint:(CGPoint)oldCenter scale:(CGFloat)oldScale -{ +{ // Step 1: restore zoom scale, first making sure it is within the allowable range. self.zoomScale = MIN(self.maximumZoomScale, MAX(self.minimumZoomScale, oldScale)); - - + + // Step 2: restore center point, first making sure it is within the allowable range. - + // 2a: convert our desired center point back to our own coordinate space CGPoint boundsCenter = [self convertPoint:oldCenter fromView:pdfView]; // 2b: calculate the content offset that would yield that center point - CGPoint offset = CGPointMake(boundsCenter.x - self.bounds.size.width / 2.0, + CGPoint offset = CGPointMake(boundsCenter.x - self.bounds.size.width / 2.0, boundsCenter.y - self.bounds.size.height / 2.0); // 2c: restore offset, adjusted to be within the allowable range CGPoint maxOffset = [self maximumContentOffset]; diff --git a/PDFViewController.h b/PDFViewController.h index e5dcd6a1..9402c1d7 100644 --- a/PDFViewController.h +++ b/PDFViewController.h @@ -15,7 +15,7 @@ NSMutableSet *recycledPages; NSMutableSet *visiblePages; - + // these values are stored off before we start rotation so we adjust our content offset appropriately during rotation int firstVisiblePageIndexBeforeRotation; CGFloat percentScrolledIntoFirstVisiblePage; diff --git a/PDFViewController.m b/PDFViewController.m index 62aaa537..a16651ec 100644 --- a/PDFViewController.m +++ b/PDFViewController.m @@ -42,7 +42,7 @@ - (void)loadView { // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)initPDFScroll { - + CGRect pagingScrollViewFrame = [self frameForPagingScrollView]; pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame]; pagingScrollView.pagingEnabled = YES; @@ -52,9 +52,9 @@ - (void)initPDFScroll { pagingScrollView.contentSize = [self contentSizeForPagingScrollView]; pagingScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; pagingScrollView.delegate = self; - + [self.view addSubview:self.pagingScrollView]; - + // Step 2: prepare to tile content recycledPages = [[NSMutableSet alloc] init]; visiblePages = [[NSMutableSet alloc] init]; @@ -103,7 +103,7 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView #pragma mark - #pragma mark View controller rotation methods -- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation +- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return YES; } @@ -114,25 +114,25 @@ - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrie // place to calculate the content offset that we will need in the new orientation // CGFloat offset = pagingScrollView.contentOffset.x; // CGFloat pageWidth = pagingScrollView.bounds.size.width; -// +// // if (offset >= 0) { // firstVisiblePageIndexBeforeRotation = floorf(offset / pageWidth); // percentScrolledIntoFirstVisiblePage = (offset - (firstVisiblePageIndexBeforeRotation * pageWidth)) / pageWidth; // } else { // firstVisiblePageIndexBeforeRotation = 0; // percentScrolledIntoFirstVisiblePage = offset / pageWidth; -// } - +// } + self.pagingScrollView.zoomScale = NO_ZOOM_SCALE; - - [[self currentlyDisplayedPage] willRotate]; + + [[self currentlyDisplayedPage] willRotate]; } - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { // recalculate contentSize based on current orientation pagingScrollView.contentSize = [self contentSizeForPagingScrollView]; - + // // adjust frames and configuration of each visible page for (PDFScrollView *page in visiblePages) { // CGPoint restorePoint = [page pointToCenterAfterRotation]; @@ -146,7 +146,7 @@ - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInte page.frame = [self frameForPageAtIndex:page.index - 1]; } - // + // // // adjust contentOffset to preserve page location based on values collected prior to location // CGFloat pageWidth = pagingScrollView.bounds.size.width; // CGFloat newOffset = (firstVisiblePageIndexBeforeRotation * pageWidth) + (percentScrolledIntoFirstVisiblePage * pageWidth); @@ -161,7 +161,7 @@ - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceO #endif // recalculate contentSize based on current orientation pagingScrollView.contentSize = [self contentSizeForPagingScrollView]; - + [[self currentlyDisplayedPage] didRotate]; } @@ -170,7 +170,7 @@ - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceO - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; - + // Release any cached data, images, etc. that aren't in use. } @@ -190,7 +190,7 @@ - (void)dealloc { #pragma mark - #pragma mark Tiling and page configuration -- (void)tilePages +- (void)tilePages { // Calculate which pages are visible CGRect visibleBounds = pagingScrollView.bounds; @@ -198,8 +198,8 @@ - (void)tilePages int lastNeededPageIndex = floorf((CGRectGetMaxX(visibleBounds)-1) / CGRectGetWidth(visibleBounds)); firstNeededPageIndex = MAX(firstNeededPageIndex, 0); lastNeededPageIndex = MIN(lastNeededPageIndex, [self pageCount] - 1); - - // Recycle no-longer-visible pages + + // Recycle no-longer-visible pages for (PDFScrollView *page in visiblePages) { if (page.index - 1 < firstNeededPageIndex || page.index - 1 > lastNeededPageIndex) { [recycledPages addObject:page]; @@ -207,7 +207,7 @@ - (void)tilePages } } [visiblePages minusSet:recycledPages]; - + // add missing pages for (int index = firstNeededPageIndex; index <= lastNeededPageIndex; index++) { if (![self isDisplayingPageForIndex:index]) { @@ -220,7 +220,7 @@ - (void)tilePages //[self setMaxMinZoomScalesForCurrentBounds]; [visiblePages addObject:page]; } - } + } } #pragma mark -