Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AssetTablePicker behaves like iOS Photos: loads scrolled to the bottom and loads quickly #24

1 change: 1 addition & 0 deletions Classes/ELCAssetTablePicker.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
@property (nonatomic, assign) ALAssetsGroup *assetGroup; @property (nonatomic, assign) ALAssetsGroup *assetGroup;
@property (nonatomic, retain) NSMutableArray *elcAssets; @property (nonatomic, retain) NSMutableArray *elcAssets;
@property (nonatomic, retain) IBOutlet UILabel *selectedAssetsLabel; @property (nonatomic, retain) IBOutlet UILabel *selectedAssetsLabel;
@property (nonatomic) BOOL reloadData;


-(int)totalSelectedAssets; -(int)totalSelectedAssets;
-(void)preparePhotos; -(void)preparePhotos;
Expand Down
79 changes: 53 additions & 26 deletions Classes/ELCAssetTablePicker.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@




@implementation ELCAssetTablePicker @implementation ELCAssetTablePicker

{
NSInteger start;
}
@synthesize parent; @synthesize parent;
@synthesize selectedAssetsLabel; @synthesize selectedAssetsLabel;
@synthesize assetGroup, elcAssets; @synthesize assetGroup, elcAssets, reloadData;


-(void)viewDidLoad { -(void)viewDidLoad {
self.reloadData = YES;
[self.tableView setSeparatorColor:[UIColor clearColor]]; [self.tableView setSeparatorColor:[UIColor clearColor]];
[self.tableView setAllowsSelection:NO]; [self.tableView setAllowsSelection:NO];


Expand All @@ -30,10 +32,35 @@ -(void)viewDidLoad {
[self.navigationItem setRightBarButtonItem:doneButtonItem]; [self.navigationItem setRightBarButtonItem:doneButtonItem];
[self.navigationItem setTitle:@"Loading..."]; [self.navigationItem setTitle:@"Loading..."];


NSInteger count = self.assetGroup.numberOfAssets;
NSInteger startNumberOfAssets = 96 + count%4;
start = MAX(0, count-startNumberOfAssets);

// Set up the first ~100 photos
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(start, count > startNumberOfAssets ? startNumberOfAssets : count)];
for (int i = 0; i < start; i++){
[self.elcAssets addObject:[NSNull null]];
}
[self.assetGroup enumerateAssetsAtIndexes:indexSet options:0 usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result == nil)
{
return;
}
ELCAsset *elcAsset = [[[ELCAsset alloc] initWithAsset:result] autorelease];
[elcAsset setParent:self];
[self.elcAssets addObject:elcAsset];
}];
[self.tableView reloadData];

[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:ceil(assetGroup.numberOfAssets / 4.0)-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];

// For some reason it only scrolls about 80% through the final image... This scrolls
// the table view all the way to the bottom. 50 is just a number thats bigger than the
// sliver of the image thats covered up.
[self.tableView setContentOffset:CGPointMake(0, self.tableView.contentOffset.y+50)];

[self performSelectorInBackground:@selector(preparePhotos) withObject:nil]; [self performSelectorInBackground:@selector(preparePhotos) withObject:nil];


// Show partial while full list loads
[self.tableView performSelector:@selector(reloadData) withObject:nil afterDelay:.5];
} }


-(void)preparePhotos { -(void)preparePhotos {
Expand All @@ -42,21 +69,19 @@ -(void)preparePhotos {




NSLog(@"enumerating photos"); NSLog(@"enumerating photos");
[self.assetGroup enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop)
{ NSIndexSet *newIndexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, start)];
if(result == nil) [self.assetGroup enumerateAssetsAtIndexes:newIndexSet options:0 usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
{ if(result == nil)
return; {
} return;
}
ELCAsset *elcAsset = [[[ELCAsset alloc] initWithAsset:result] autorelease]; ELCAsset *elcAsset = [[[ELCAsset alloc] initWithAsset:result] autorelease];
[elcAsset setParent:self]; [elcAsset setParent:self];
[self.elcAssets addObject:elcAsset]; [self.elcAssets replaceObjectAtIndex:index withObject:elcAsset];
}]; }];
NSLog(@"done enumerating photos"); NSLog(@"done enumerating photos");

[self.navigationItem performSelectorOnMainThread:@selector(setTitle:) withObject:@"Pick Photos" waitUntilDone:NO];
[self.tableView reloadData];
[self.navigationItem setTitle:@"Pick Photos"];


[pool release]; [pool release];


Expand All @@ -65,15 +90,14 @@ -(void)preparePhotos {
- (void) doneAction:(id)sender { - (void) doneAction:(id)sender {


NSMutableArray *selectedAssetsImages = [[[NSMutableArray alloc] init] autorelease]; NSMutableArray *selectedAssetsImages = [[[NSMutableArray alloc] init] autorelease];
NSArray *currentlyLoadedAssets = [self.elcAssets copy];
for(ELCAsset *elcAsset in self.elcAssets) for(ELCAsset *elcAsset in currentlyLoadedAssets)
{ {
if([elcAsset selected]) { if(elcAsset != (id)[NSNull null] && [elcAsset selected]) {


[selectedAssetsImages addObject:[elcAsset asset]]; [selectedAssetsImages addObject:[elcAsset asset]];
} }
} }

[(ELCAlbumPickerController*)self.parent selectedAssets:selectedAssetsImages]; [(ELCAlbumPickerController*)self.parent selectedAssets:selectedAssetsImages];
} }


Expand All @@ -86,7 +110,7 @@ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return ceil([self.assetGroup numberOfAssets] / 4.0); return ceil(assetGroup.numberOfAssets / 4.0);
} }


- (NSArray*)assetsForIndexPath:(NSIndexPath*)_indexPath { - (NSArray*)assetsForIndexPath:(NSIndexPath*)_indexPath {
Expand Down Expand Up @@ -135,14 +159,17 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N


ELCAssetCell *cell = (ELCAssetCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; ELCAssetCell *cell = (ELCAssetCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];


NSMutableArray *assets = [[self assetsForIndexPath:indexPath] mutableCopy];
[assets removeObjectIdenticalTo:[NSNull null]];
if (cell == nil) if (cell == nil)
{ {
cell = [[[ELCAssetCell alloc] initWithAssets:[self assetsForIndexPath:indexPath] reuseIdentifier:CellIdentifier] autorelease]; cell = [[[ELCAssetCell alloc] initWithAssets:assets reuseIdentifier:CellIdentifier] autorelease];
} }
else else
{ {
[cell setAssets:[self assetsForIndexPath:indexPath]]; [cell setAssets:assets];
} }



return cell; return cell;
} }
Expand Down