Professional Documents
Culture Documents
// ELCAssetTablePicker.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"
#import "ELCConsole.h"
@interface ELCAssetTablePicker ()
@end
@implementation ELCAssetTablePicker
- (id)init
{
self = [super init];
if (self) {
//Sets a reasonable default bigger then 0 for columns
//So that we don't have a divide by 0 scenario
self.columns = 4;
}
return self;
}
- (void)viewDidLoad
{
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[self.tableView setAllowsSelection:NO];
if (self.immediateReturn) {
} else {
UIBarButtonItem *doneButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self
action:@selector(doneAction:)];
[self.navigationItem setRightBarButtonItem:doneButtonItem];
[self.navigationItem setTitle:NSLocalizedString(@"Loading...", nil)];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[ELCConsole mainConsole] removeAllIndex];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:ALAssetsLibraryChangedNotification object:nil];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}
- (void)didRotateFromInterfaceOrientation:
(UIInterfaceOrientation)fromInterfaceOrientation
{
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
self.columns = self.view.bounds.size.width / 80;
[self.tableView reloadData];
}
- (void)preparePhotos
{
@autoreleasepool {
[self.elcAssets removeAllObjects];
[self.assetGroup enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger
index, BOOL *stop) {
if (result == nil) {
return;
}
if (!isAssetFiltered) {
[self.elcAssets addObject:elcAsset];
}
}];
dispatch_sync(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
// scroll to bottom
long section = [self numberOfSectionsInTableView:self.tableView] - 1;
long 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 ?
NSLocalizedString(@"Pick Photo", nil) : NSLocalizedString(@"Pick Photos", nil)];
});
}
}
- (void)doneAction:(id)sender
{
NSMutableArray *selectedAssetsImages = [[NSMutableArray alloc] init];
- (BOOL)shouldSelectAsset:(ELCAsset *)asset
{
NSUInteger selectionCount = 0;
for (ELCAsset *elcAsset in self.elcAssets) {
if (elcAsset.selected) selectionCount++;
}
BOOL shouldSelect = YES;
if ([self.parent
respondsToSelector:@selector(shouldSelectAsset:previousCount:)]) {
shouldSelect = [self.parent shouldSelectAsset:asset
previousCount:selectionCount];
}
return shouldSelect;
}
- (void)assetSelected:(ELCAsset *)asset
{
if (self.singleSelection) {
- (BOOL)shouldDeselectAsset:(ELCAsset *)asset
{
if (self.immediateReturn){
return NO;
}
return YES;
}
- (void)assetDeselected:(ELCAsset *)asset
{
if (self.singleSelection) {
for (ELCAsset *elcAsset in self.elcAssets) {
if (asset != elcAsset) {
elcAsset.selected = NO;
}
}
}
if (self.immediateReturn) {
NSArray *singleAssetArray = @[asset.asset];
[(NSObject *)self.parent performSelector:@selector(selectedAssets:)
withObject:singleAssetArray afterDelay:0];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
if (cell == nil) {
cell = [[ELCAssetCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
return cell;
}
- (int)totalSelectedAssets
{
int count = 0;
return count;
}
@end