Skip to content

Commit

Permalink
cache page images for responsiveness
Browse files Browse the repository at this point in the history
  • Loading branch information
brow committed Apr 29, 2010
1 parent a92621a commit b6200ba
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
3 changes: 3 additions & 0 deletions Leaves/LeavesView.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
id<LeavesViewDelegate> delegate;

CGSize pageSize;
NSMutableDictionary *pageCache;

CGPoint touchBeganPoint;
BOOL touchIsActive;
CGRect nextPageRect, prevPageRect;
Expand All @@ -40,6 +42,7 @@

@property (assign) id<LeavesViewDataSource> dataSource;
@property (assign) id<LeavesViewDelegate> delegate;
@property (readonly) CGFloat targetWidth;

- (void) reloadData;

Expand Down
54 changes: 42 additions & 12 deletions Leaves/LeavesView.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,22 @@ - (void) setUpLayers {
self.leafEdge = 1.0;
}

- (void) initialize {
pageCache = [[NSMutableDictionary alloc] init];
}

- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
[self setUpLayers];
[self initialize];
}
return self;
}

- (void) awakeFromNib {
[super awakeFromNib];
[self setUpLayers];
[self initialize];
}

- (void)dealloc {
Expand All @@ -107,9 +113,18 @@ - (void)dealloc {
[topPageReverseShading release];
[bottomPage release];
[bottomPageShadow release];

[pageCache release];

[super dealloc];
}

- (void) reloadData {
[pageCache removeAllObjects];
numberOfPages = [dataSource numberOfPagesInLeavesView:self];
self.currentPageIndex = 0;
}

- (CGImageRef) imageForPageIndex:(NSUInteger)pageIndex {
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL,
Expand All @@ -133,17 +148,31 @@ - (CGImageRef) imageForPageIndex:(NSUInteger)pageIndex {
return image;
}

- (void) reloadData {
numberOfPages = [dataSource numberOfPagesInLeavesView:self];
self.currentPageIndex = 0;
- (CGImageRef) cachedImageForPageIndex:(NSUInteger)pageIndex {
NSNumber *pageIndexNumber = [NSNumber numberWithInt:pageIndex];
UIImage *pageImage = [pageCache objectForKey:pageIndexNumber];
if (!pageImage) {
CGImageRef pageCGImage = [self imageForPageIndex:pageIndex];
pageImage = [UIImage imageWithCGImage:pageCGImage];
[pageCache setObject:pageImage forKey:pageIndexNumber];
}
return pageImage.CGImage;
}

- (void) cleanCache {
/* Uncache all pages except previous, current, and next. */
for (NSNumber *key in [pageCache allKeys])
if (ABS([key intValue] - (int)currentPageIndex) > 1)
[pageCache removeObjectForKey:key];
}

- (void) reloadImages {
- (void) getImages {
if (currentPageIndex < numberOfPages) {
topPage.contents = (id)[self imageForPageIndex:currentPageIndex];
topPageReverseImage.contents = (id)[self imageForPageIndex:currentPageIndex];
topPage.contents = (id)[self cachedImageForPageIndex:currentPageIndex];
topPageReverseImage.contents = (id)[self cachedImageForPageIndex:currentPageIndex];
if (currentPageIndex < numberOfPages - 1)
bottomPage.contents = (id)[self imageForPageIndex:currentPageIndex + 1];
bottomPage.contents = (id)[self cachedImageForPageIndex:currentPageIndex + 1];
[self cleanCache];
} else {
topPage.contents = nil;
topPageReverseImage.contents = nil;
Expand Down Expand Up @@ -191,7 +220,7 @@ - (void) didTurnPageBackward {

- (void) didTurnPageForward {
interactionLocked = NO;
self.currentPageIndex = self.currentPageIndex + 1;
self.currentPageIndex = self.currentPageIndex + 1;
[self didTurnToPageAtIndex:currentPageIndex];
}

Expand Down Expand Up @@ -238,7 +267,7 @@ - (void) setCurrentPageIndex:(NSUInteger)aCurrentPageIndex {
[CATransaction setValue:(id)kCFBooleanTrue
forKey:kCATransactionDisableActions];

[self reloadImages];
[self getImages];

self.leafEdge = 1.0;

Expand Down Expand Up @@ -301,15 +330,15 @@ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
interactionLocked = YES;
[self performSelector:@selector(didTurnPageForward)
withObject:nil
afterDelay:duration + 0.2];
afterDelay:duration + 0.25];
}
else {
self.leafEdge = 1.0;
duration = 1 - leafEdge;
interactionLocked = YES;
[self performSelector:@selector(didTurnPageBackward)
withObject:nil
afterDelay:duration + 0.2];
afterDelay:duration + 0.25];
}
[CATransaction setValue:[NSNumber numberWithFloat:duration]
forKey:kCATransactionAnimationDuration];
Expand All @@ -329,7 +358,8 @@ - (void) layoutSubviews {
[self setLayerFrames];
[CATransaction commit];

[self reloadImages];
[pageCache removeAllObjects];
[self getImages];

CGFloat touchRectsWidth = self.bounds.size.width / 7;
nextPageRect = CGRectMake(self.bounds.size.width - touchRectsWidth,
Expand Down

0 comments on commit b6200ba

Please sign in to comment.