Permalink
Browse files

added some caching... not sure how performant it is (works well on a …

…4s, not sure what that really says though.)
  • Loading branch information...
1 parent 087a370 commit 7c19118f8cfdc1b16eed7a842ff2dd08e5b264fb @kolinkrewinkel committed Apr 11, 2012
Showing with 32 additions and 6 deletions.
  1. +31 −5 Examples/GridViewDemo/GridViewDemo/GridViewDemoViewController.m
  2. +1 −1 KKGridView/KKGridViewCell.m
@@ -8,6 +8,7 @@
#import "GridViewDemoViewController.h"
#import <QuartzCore/QuartzCore.h>
+#import <AssetsLibrary/AssetsLibrary.h>
#import <KKGridView/KKGridView.h>
#import <KKGridView/KKGridViewCell.h>
#import <KKGridView/KKIndexPath.h>
@@ -18,8 +19,8 @@ @implementation GridViewDemoViewController {
ALAssetsLibrary *_assetsLibrary;
NSMutableArray *_photoGroups;
NSMutableArray *_assets;
- NSDictionary *_thumbnailCache;
- dispatch_queue _imageQueue;
+ NSCache *_thumbnailCache;
+ dispatch_queue_t _imageQueue;
}
@synthesize firstSectionCount = _firstSectionCount;
@@ -30,7 +31,11 @@ - (void)loadView
{
[super loadView];
+ self.title = @"Photos | GridViewDemo";
+
+ // Create the assets library object; retain it to deal with iOS's retardation.
_assetsLibrary = [[ALAssetsLibrary alloc] init];
+ // Enumerate through the user's photos.
[_assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (!_photoGroups)
_photoGroups = [[NSMutableArray alloc] init];
@@ -42,22 +47,43 @@ - (void)loadView
if (!_assets)
_assets = [[NSMutableArray alloc] init];
+ // More enumeration bullshit.
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (result)
[tempArray addObject:result];
+ else
+ [_assets addObject:tempArray];
}];
- [_assets addObject:tempArray];
}
[self.gridView reloadData];
+ _imageQueue = dispatch_queue_create("com.kolinkrewinkel.GridViewDemo", NULL);
+
+ dispatch_sync(_imageQueue, ^(void) {
+ if (!_thumbnailCache)
+ _thumbnailCache = [[NSCache alloc] init]; // Thanks @indragie.
+
+ NSUInteger section = 0;
+ for (NSMutableArray *array in _assets) {
+ NSUInteger index = 0;
+ for (ALAsset *asset in array) {
+ [_thumbnailCache setObject:[UIImage imageWithCGImage:[asset thumbnail]] forKey:asset]; // Store it!
+ NSLog(@"%@", [_thumbnailCache objectForKey:asset]);
+ dispatch_async(dispatch_get_main_queue(), ^{
+ [self.gridView reloadItemsAtIndexPaths:[NSArray arrayWithObject:[KKIndexPath indexPathForIndex:index inSection:section]]];
+ });
+ index++;
+ }
+ section++;
+ }
+ });
}
} failureBlock:^(NSError *error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"LOLWAT" message:[NSString stringWithFormat:@"%@", [error localizedDescription]] delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[alert show];
}];
- self.title = @"Photos | GridViewDemo";
}
@@ -76,7 +102,7 @@ - (NSUInteger)gridView:(KKGridView *)gridView numberOfItemsInSection:(NSUInteger
- (KKGridViewCell *)gridView:(KKGridView *)gridView cellForItemAtIndexPath:(KKIndexPath *)indexPath
{
KKGridViewCell *cell = [KKGridViewCell cellForGridView:gridView];
- cell.imageView.image = [UIImage imageWithCGImage:[[[_assets objectAtIndex:indexPath.section] objectAtIndex:indexPath.index] thumbnail]];
+ cell.imageView.image = [_thumbnailCache objectForKey:[[_assets objectAtIndex:indexPath.section] objectAtIndex:indexPath.index]];
return cell;
}
@@ -73,7 +73,7 @@ - (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier
_imageView = [[UIImageView alloc] initWithFrame:self.bounds];
_imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
- [self addSubview:_imageView];
+ [self.contentView addSubview:_imageView];
[_contentView addObserver:self forKeyPath:@"backgroundColor" options:NSKeyValueObservingOptionNew context:NULL];
}

0 comments on commit 7c19118

Please sign in to comment.