mirror of
https://gitee.com/shuto/cordova-imagePicker.git
synced 2026-05-23 00:05:03 +08:00
665f351a70
I got rid of the lines between the albums. Also I put some padding around the album thumbnails. See this screenshot for the improvements: http://monosnap.com/image/wcBd9YW65Y0AWFAVhUy0xgLoUx0pV5 I have created a similar pull request for the original ELCImagePickerController project: https://github.com/B-Sides/ELCImagePickerController/pull/98
180 lines
6.1 KiB
Objective-C
180 lines
6.1 KiB
Objective-C
//
|
|
// AlbumPickerController.m
|
|
//
|
|
// Created by ELC on 2/15/11.
|
|
// Copyright 2011 ELC Technologies. All rights reserved.
|
|
//
|
|
|
|
#import "ELCAlbumPickerController.h"
|
|
#import "ELCImagePickerController.h"
|
|
#import "ELCAssetTablePicker.h"
|
|
|
|
@interface ELCAlbumPickerController ()
|
|
|
|
@property (nonatomic, strong) ALAssetsLibrary *library;
|
|
|
|
@end
|
|
|
|
@implementation ELCAlbumPickerController
|
|
|
|
//Using auto synthesizers
|
|
|
|
#pragma mark -
|
|
#pragma mark View lifecycle
|
|
|
|
- (void)viewDidLoad
|
|
{
|
|
[super viewDidLoad];
|
|
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
|
|
|
|
[self.navigationItem setTitle:@"Loading..."];
|
|
|
|
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self.parent action:@selector(cancelImagePicker)];
|
|
[self.navigationItem setRightBarButtonItem:cancelButton];
|
|
|
|
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
|
|
self.assetGroups = tempArray;
|
|
|
|
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
|
|
self.library = assetLibrary;
|
|
|
|
// Load Albums into assetGroups
|
|
dispatch_async(dispatch_get_main_queue(), ^
|
|
{
|
|
@autoreleasepool {
|
|
|
|
// Group enumerator Block
|
|
void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop)
|
|
{
|
|
if (group == nil) {
|
|
return;
|
|
}
|
|
|
|
// added fix for camera albums order
|
|
NSString *sGroupPropertyName = (NSString *)[group valueForProperty:ALAssetsGroupPropertyName];
|
|
NSUInteger nType = [[group valueForProperty:ALAssetsGroupPropertyType] intValue];
|
|
|
|
if ([[sGroupPropertyName lowercaseString] isEqualToString:@"camera roll"] && nType == ALAssetsGroupSavedPhotos) {
|
|
[self.assetGroups insertObject:group atIndex:0];
|
|
}
|
|
else {
|
|
[self.assetGroups addObject:group];
|
|
}
|
|
|
|
// Reload albums
|
|
[self performSelectorOnMainThread:@selector(reloadTableView) withObject:nil waitUntilDone:YES];
|
|
};
|
|
|
|
// Group Enumerator Failure Block
|
|
void (^assetGroupEnumberatorFailure)(NSError *) = ^(NSError *error) {
|
|
|
|
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat:@"Album Error: %@ - %@", [error localizedDescription], [error localizedRecoverySuggestion]] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
|
|
[alert show];
|
|
|
|
NSLog(@"A problem occured %@", [error description]);
|
|
};
|
|
|
|
// Enumerate Albums
|
|
[self.library enumerateGroupsWithTypes:ALAssetsGroupAll
|
|
usingBlock:assetGroupEnumerator
|
|
failureBlock:assetGroupEnumberatorFailure];
|
|
|
|
}
|
|
});
|
|
}
|
|
|
|
- (void)reloadTableView
|
|
{
|
|
[self.tableView reloadData];
|
|
[self.navigationItem setTitle:@"Select an Album"];
|
|
}
|
|
|
|
- (BOOL)shouldSelectAsset:(ELCAsset *)asset previousCount:(NSUInteger)previousCount
|
|
{
|
|
return [self.parent shouldSelectAsset:asset previousCount:previousCount];
|
|
}
|
|
|
|
- (void)selectedAssets:(NSArray*)assets
|
|
{
|
|
[_parent selectedAssets:assets];
|
|
}
|
|
|
|
#pragma mark -
|
|
#pragma mark Table view data source
|
|
|
|
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
|
|
{
|
|
// Return the number of sections.
|
|
return 1;
|
|
}
|
|
|
|
|
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
|
|
{
|
|
// Return the number of rows in the section.
|
|
return [self.assetGroups count];
|
|
}
|
|
|
|
|
|
// Customize the appearance of table view cells.
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
|
|
{
|
|
static NSString *CellIdentifier = @"Cell";
|
|
|
|
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
|
|
if (cell == nil) {
|
|
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
|
|
}
|
|
|
|
// Get count
|
|
ALAssetsGroup *g = (ALAssetsGroup*)[self.assetGroups objectAtIndex:indexPath.row];
|
|
[g setAssetsFilter:[ALAssetsFilter allPhotos]];
|
|
NSInteger gCount = [g numberOfAssets];
|
|
|
|
cell.textLabel.text = [NSString stringWithFormat:@"%@ (%ld)",[g valueForProperty:ALAssetsGroupPropertyName], (long)gCount];
|
|
UIImage* image = [UIImage imageWithCGImage:[(ALAssetsGroup*)[self.assetGroups objectAtIndex:indexPath.row] posterImage]];
|
|
image = [self resize:image to:CGSizeMake(78, 78)];
|
|
[cell.imageView setImage:image];
|
|
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
|
|
|
|
return cell;
|
|
}
|
|
|
|
// Resize a UIImage. From http://stackoverflow.com/questions/2658738/the-simplest-way-to-resize-an-uiimage
|
|
- (UIImage *)resize:(UIImage *)image to:(CGSize)newSize {
|
|
//UIGraphicsBeginImageContext(newSize);
|
|
// In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
|
|
// Pass 1.0 to force exact pixel size.
|
|
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
|
|
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
|
|
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
|
|
UIGraphicsEndImageContext();
|
|
return newImage;
|
|
}
|
|
|
|
#pragma mark -
|
|
#pragma mark Table view delegate
|
|
|
|
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
|
|
{
|
|
ELCAssetTablePicker *picker = [[ELCAssetTablePicker alloc] initWithNibName: nil bundle: nil];
|
|
picker.parent = self;
|
|
|
|
picker.assetGroup = [self.assetGroups objectAtIndex:indexPath.row];
|
|
[picker.assetGroup setAssetsFilter:[ALAssetsFilter allPhotos]];
|
|
|
|
picker.assetPickerFilterDelegate = self.assetPickerFilterDelegate;
|
|
picker.immediateReturn = self.immediateReturn;
|
|
picker.singleSelection = self.singleSelection;
|
|
|
|
[self.navigationController pushViewController:picker animated:YES];
|
|
}
|
|
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
|
|
{
|
|
return 95;
|
|
}
|
|
|
|
@end
|
|
|