Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: 19147c008a
Fetching contributors…

Cannot retrieve contributors at this time

206 lines (157 sloc) 5.793 kB
//
// AssetTablePicker.m
//
// Created by ELC on 2/15/11.
// Copyright 2011 ELC Technologies. All rights reserved.
//
#import "ELCAssetTablePicker.h"
#import "ELCAssetCell.h"
#import "ELCAsset.h"
#import "ELCAlbumPickerController.h"
@interface ELCAssetTablePicker ()
@property (nonatomic, assign) int columns;
@end
@implementation ELCAssetTablePicker
@synthesize parent = _parent;;
@synthesize selectedAssetsLabel = _selectedAssetsLabel;
@synthesize assetGroup = _assetGroup;
@synthesize elcAssets = _elcAssets;
@synthesize singleSelection = _singleSelection;
@synthesize columns = _columns;
- (void)viewDidLoad
{
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[self.tableView setAllowsSelection:NO];
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
self.elcAssets = tempArray;
[tempArray release];
if (self.immediateReturn) {
} else {
UIBarButtonItem *doneButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneAction:)] autorelease];
[self.navigationItem setRightBarButtonItem:doneButtonItem];
[self.navigationItem setTitle:@"Loading..."];
}
[self performSelectorInBackground:@selector(preparePhotos) withObject:nil];
// Show partial while full list loads
// [self.tableView performSelector:@selector(reloadData) withObject:nil afterDelay:.5];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.columns = self.view.bounds.size.width / 80;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
return YES;
} else {
return toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
}
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
self.columns = self.view.bounds.size.width / 80;
[self.tableView reloadData];
}
- (void)preparePhotos
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(@"enumerating photos");
[self.assetGroup enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result == nil) {
return;
}
ELCAsset *elcAsset = [[ELCAsset alloc] initWithAsset:result];
[elcAsset setParent:self];
[self.elcAssets addObject:elcAsset];
[elcAsset release];
}];
NSLog(@"done enumerating photos");
dispatch_sync(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
// scroll to bottom
int section = [self numberOfSectionsInTableView:self.tableView] - 1;
int row = [self tableView:self.tableView numberOfRowsInSection:section] - 1;
if (section >= 0 && row >= 0) {
NSIndexPath *ip = [NSIndexPath indexPathForRow:row
inSection:section];
[self.tableView scrollToRowAtIndexPath:ip
atScrollPosition:UITableViewScrollPositionBottom
animated:NO];
}
[self.navigationItem setTitle:self.singleSelection ? @"Pick Photo" : @"Pick Photos"];
});
[pool release];
}
- (void)doneAction:(id)sender
{
NSMutableArray *selectedAssetsImages = [[[NSMutableArray alloc] init] autorelease];
for(ELCAsset *elcAsset in self.elcAssets) {
if([elcAsset selected]) {
[selectedAssetsImages addObject:[elcAsset asset]];
}
}
[self.parent selectedAssets:selectedAssetsImages];
}
- (void)assetSelected:(id)asset
{
if (self.singleSelection) {
for(ELCAsset *elcAsset in self.elcAssets) {
if(asset != elcAsset) {
elcAsset.selected = NO;
}
}
}
if (self.immediateReturn) {
NSArray *singleAssetArray = [NSArray arrayWithObject:[asset asset]];
[(NSObject *)self.parent performSelector:@selector(selectedAssets:) withObject:singleAssetArray afterDelay:0];
}
}
#pragma mark UITableViewDataSource Delegate Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return ceil([self.elcAssets count] / (float)self.columns);
}
- (NSArray *)assetsForIndexPath:(NSIndexPath *)path
{
int index = path.row * self.columns;
int length = MIN(self.columns, [self.elcAssets count] - index);
return [self.elcAssets subarrayWithRange:NSMakeRange(index, length)];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
ELCAssetCell *cell = (ELCAssetCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[ELCAssetCell alloc] initWithAssets:[self assetsForIndexPath:indexPath] reuseIdentifier:CellIdentifier] autorelease];
} else {
[cell setAssets:[self assetsForIndexPath:indexPath]];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 79;
}
- (int)totalSelectedAssets {
int count = 0;
for(ELCAsset *asset in self.elcAssets) {
if([asset selected]) {
count++;
}
}
return count;
}
- (void)dealloc
{
[_elcAssets release];
[_selectedAssetsLabel release];
[super dealloc];
}
@end
Jump to Line
Something went wrong with that request. Please try again.