Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Return the best version available of the image and not a platform dependent version. #32

Closed
wants to merge 6 commits into from
Commits on Nov 19, 2012
  1. @brutella
Commits on Dec 20, 2012
  1. @brutella
Commits on Jan 2, 2013
  1. @brutella
Commits on Jan 3, 2013
  1. @brutella
  2. @brutella

    Code cleanup

    brutella authored
Commits on Jan 26, 2013
  1. @brutella
This page is out of date. Refresh to see the latest.
View
1  Classes/ELCAlbumPickerController.h
@@ -7,6 +7,7 @@
#import <UIKit/UIKit.h>
#import <AssetsLibrary/AssetsLibrary.h>
+#import "ELCAssetTablePicker.h"
@interface ELCAlbumPickerController : UITableViewController {
View
8 Classes/ELCAlbumPickerController.m
@@ -19,7 +19,7 @@ @implementation ELCAlbumPickerController
- (void)viewDidLoad {
[super viewDidLoad];
- [self.navigationItem setTitle:@"Loading..."];
+ [self.navigationItem setTitle:NSLocalizedString(@"Loading...", @"Loading...")];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self.parent action:@selector(cancelImagePicker)];
[self.navigationItem setRightBarButtonItem:cancelButton];
@@ -72,7 +72,7 @@ - (void)viewDidLoad {
-(void)reloadTableView {
[self.tableView reloadData];
- [self.navigationItem setTitle:@"Select an Album"];
+ [self.navigationItem setTitle:NSLocalizedString(@"Select an Album", @"Select an Album")];
}
-(void)selectedAssets:(NSArray*)_assets {
@@ -107,7 +107,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
// Get count
ALAssetsGroup *g = (ALAssetsGroup*)[assetGroups objectAtIndex:indexPath.row];
- [g setAssetsFilter:[ALAssetsFilter allPhotos]];
+ [g setAssetsFilter:[ALAssetsFilter allAssets]];
NSInteger gCount = [g numberOfAssets];
cell.textLabel.text = [NSString stringWithFormat:@"%@ (%d)",[g valueForProperty:ALAssetsGroupPropertyName], gCount];
@@ -127,7 +127,7 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
// Move me
picker.assetGroup = [assetGroups objectAtIndex:indexPath.row];
- [picker.assetGroup setAssetsFilter:[ALAssetsFilter allPhotos]];
+ [picker.assetGroup setAssetsFilter:[ALAssetsFilter allAssets]];
[self.navigationController pushViewController:picker animated:YES];
[picker release];
View
3  Classes/ELCAsset.h
@@ -20,6 +20,7 @@
@property (nonatomic, assign) id parent;
-(id)initWithAsset:(ALAsset*)_asset;
--(BOOL)selected;
+-(BOOL) selected;
+- (void)setDisabled:(BOOL)disabledFlag;
@end
View
178 Classes/ELCAsset.m
@@ -8,6 +8,12 @@
#import "ELCAsset.h"
#import "ELCAssetTablePicker.h"
+@interface ELCAsset ()
+
+@property (nonatomic, strong) UIView *disabledView;
+
+@end
+
@implementation ELCAsset
@synthesize asset;
@@ -33,6 +39,44 @@ -(id)initWithAsset:(ALAsset*)_asset {
[assetImageView setImage:[UIImage imageWithCGImage:[self.asset thumbnail]]];
[self addSubview:assetImageView];
[assetImageView release];
+
+ if ([_asset valueForProperty:ALAssetPropertyType] == ALAssetTypeVideo)
+ { // Show duration in thumbnail
+ int viewHeight = 16;
+ UIView *durationContainerView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(viewFrames) - viewHeight, CGRectGetWidth(viewFrames), viewHeight)];
+ durationContainerView.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.6f];
+ [self addSubview:durationContainerView];
+
+ UILabel *durationLabel = [[UILabel alloc] initWithFrame:CGRectZero];
+ durationLabel.backgroundColor = [UIColor clearColor];
+ durationLabel.textColor = [UIColor whiteColor];
+ durationLabel.font = [UIFont boldSystemFontOfSize:12];
+ [durationContainerView addSubview:durationLabel];
+
+ double duration = [[_asset valueForProperty:ALAssetPropertyDuration] doubleValue];
+ NSDateFormatter *durationFormatter = [[NSDateFormatter alloc] init];
+ [durationFormatter setDateFormat:@"mm:ss"];
+ durationLabel.text = [durationFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:duration]];
+ [durationLabel sizeToFit];
+
+ durationLabel.center = CGPointMake(0, CGRectGetHeight(durationContainerView.bounds)/2);
+
+ CGRect durationFrame = durationLabel.bounds;
+ CGRect containerContentRect = CGRectInset(durationContainerView.frame, 4, 2);
+ durationFrame.origin.x = CGRectGetMaxX(containerContentRect) - CGRectGetWidth(durationFrame);
+
+ durationLabel.frame = CGRectIntegral(durationFrame);
+
+ CGSize imageSize = [self cameraImageSize];
+ UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,imageSize.width, imageSize.height)];
+ imageView.image = [self cameraImage];
+ imageView.contentMode = UIViewContentModeScaleAspectFit;
+
+ // Layouting
+ [durationContainerView addSubview:imageView];
+ imageView.center = CGPointMake(CGRectGetMinX(containerContentRect) + CGRectGetMidX(imageView.bounds), CGRectGetMidY(durationContainerView.bounds));
+ imageView.frame = CGRectIntegral(imageView.frame);
+ }
overlayView = [[UIImageView alloc] initWithFrame:viewFrames];
[overlayView setImage:[UIImage imageNamed:@"Overlay.png"]];
@@ -44,17 +88,16 @@ -(id)initWithAsset:(ALAsset*)_asset {
}
-(void)toggleSelection {
+ if([(ELCAssetTablePicker*)self.parent canSelectAsset:self.asset])
+ {
+ overlayView.hidden = !overlayView.hidden;
+ if (!overlayView.hidden) {
+ [self.parent didSelectAsset:self.asset];
+ } else {
+ [self.parent didDeselectAsset:self.asset];
+ }
- overlayView.hidden = !overlayView.hidden;
-
-// if([(ELCAssetTablePicker*)self.parent totalSelectedAssets] >= 10) {
-//
-// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Maximum Reached" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
-// [alert show];
-// [alert release];
-//
-// [(ELCAssetTablePicker*)self.parent doneAction:nil];
-// }
+ }
}
-(BOOL)selected {
@@ -67,12 +110,123 @@ -(void)setSelected:(BOOL)_selected {
[overlayView setHidden:!_selected];
}
-- (void)dealloc
-{
+#define kDisabledTag 1
+
+- (void)setDisabled:(BOOL)disabledFlag
+{
+ [[self viewWithTag:kDisabledTag] removeFromSuperview];
+ if (disabledFlag) {
+ _disabledView = [[UIView alloc] initWithFrame:CGRectZero];
+ _disabledView.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.7f];
+ _disabledView.tag = kDisabledTag;
+ [self addSubview:_disabledView];
+ }
+}
+
+- (void)layoutSubviews
+{
+ [super layoutSubviews];
+
+ _disabledView.frame = self.bounds;
+}
+
+- (void)dealloc
+{
self.asset = nil;
[overlayView release];
[super dealloc];
}
+#pragma mark - Private
+
+- (CGSize)cameraImageSize
+{
+ // width 38, height 20 <= retina
+ return CGSizeMake(14, 8);
+}
+
+- (NSData *)dataWithBase64EncodedString:(NSString *)string {
+ //
+ // NSData+Base64.m
+ //
+ // Version 1.0.2
+ //
+ // Created by Nick Lockwood on 12/01/2012.
+ // Copyright (C) 2012 Charcoal Design
+ //
+ // Distributed under the permissive zlib License
+ // Get the latest version from here:
+ //
+ // https://github.com/nicklockwood/Base64
+ //
+ // This software is provided 'as-is', without any express or implied
+ // warranty. In no event will the authors be held liable for any damages
+ // arising from the use of this software.
+ //
+ // Permission is granted to anyone to use this software for any purpose,
+ // including commercial applications, and to alter it and redistribute it
+ // freely, subject to the following restrictions:
+ //
+ // 1. The origin of this software must not be misrepresented; you must not
+ // claim that you wrote the original software. If you use this software
+ // in a product, an acknowledgment in the product documentation would be
+ // appreciated but is not required.
+ //
+ // 2. Altered source versions must be plainly marked as such, and must not be
+ // misrepresented as being the original software.
+ //
+ // 3. This notice may not be removed or altered from any source distribution.
+ //
+ const char lookup[] = {
+ 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
+ 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
+ 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 62, 99, 99, 99, 63,
+ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 99, 99, 99, 99, 99, 99,
+ 99, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
+ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 99, 99, 99, 99, 99,
+ 99, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
+ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 99, 99, 99, 99, 99
+ };
+
+ NSData *inputData = [string dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
+ long long inputLength = [inputData length];
+ const unsigned char *inputBytes = [inputData bytes];
+
+ long long maxOutputLength = (inputLength / 4 + 1) * 3;
+ NSMutableData *outputData = [NSMutableData dataWithLength:maxOutputLength];
+ unsigned char *outputBytes = (unsigned char *)[outputData mutableBytes];
+
+ int accumulator = 0;
+ long long outputLength = 0;
+ unsigned char accumulated[] = {0, 0, 0, 0};
+ for (long long i = 0; i < inputLength; i++) {
+ unsigned char decoded = lookup[inputBytes[i] & 0x7F];
+ if (decoded != 99) {
+ accumulated[accumulator] = decoded;
+ if (accumulator == 3) {
+ outputBytes[outputLength++] = (accumulated[0] << 2) | (accumulated[1] >> 4);
+ outputBytes[outputLength++] = (accumulated[1] << 4) | (accumulated[2] >> 2);
+ outputBytes[outputLength++] = (accumulated[2] << 6) | accumulated[3];
+ }
+ accumulator = (accumulator + 1) % 4;
+ }
+ }
+
+ //handle left-over data
+ if (accumulator > 0) outputBytes[outputLength] = (accumulated[0] << 2) | (accumulated[1] >> 4);
+ if (accumulator > 1) outputBytes[++outputLength] = (accumulated[1] << 4) | (accumulated[2] >> 2);
+ if (accumulator > 2) outputLength++;
+
+ //truncate data to match actual output length
+ outputData.length = outputLength;
+ return outputLength? outputData: nil;
+}
+
+- (UIImage *)cameraImage
+{
+ NSString *base64EncodedImageString = @"iVBORw0KGgoAAAANSUhEUgAAABwAAAAQCAYAAAAFzx/vAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccll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4MTEyMDY4MTE4REJCQTVFMTg1RDRCOEEyIi8+IDwvcmRmOkRlc2NyaXB0aW9uPiA8L3JkZjpSREY+IDwveDp4bXBtZXRhPiA8P3hwYWNrZXQgZW5kPSJyIj8+3806ewAAAMdJREFUeNpi+P//vxAQX/pPGegFYgY82AaIvwFxJogz5z91gBMeyz5D1WQyMTAw2DBQB9hjEbME4u1AzAMTAFnISSUL2QhZBrOQFsAMahk/ugQtLDQG4l3YLKOFhSDL9uKyjBYWcmKJS5paeASIPYH4O70sBIGDQOyPy1JapdLduCyllYUwS4OA+Be9LASBHeiWgix8QSXDX+MQ3wrEETBLQRYupoJloLhaj0ceJBcNthRYgjMDcQsQ/ySzlniIp6ZAx2wAAQYAhnhI8eeyV8IAAAAASUVORK5CYII=";
+ return [[UIImage alloc] initWithData:[self dataWithBase64EncodedString:base64EncodedImageString]];
+}
+
@end
View
4 Classes/ELCAssetTablePicker.h
@@ -27,7 +27,9 @@
-(int)totalSelectedAssets;
-(void)preparePhotos;
-
-(void)doneAction:(id)sender;
+- (BOOL)canSelectAsset:(ALAsset *)asset;
+- (void)didDeselectAsset:(ALAsset *)asset;
+- (void)didSelectAsset:(ALAsset *)asset;
@end
View
54 Classes/ELCAssetTablePicker.m
@@ -10,6 +10,11 @@
#import "ELCAsset.h"
#import "ELCAlbumPickerController.h"
+@interface ELCAssetTablePicker ()
+
+@property (nonatomic, strong) NSMutableSet *selectedAssets;
+
+@end
@implementation ELCAssetTablePicker
@@ -18,7 +23,7 @@ @implementation ELCAssetTablePicker
@synthesize assetGroup, elcAssets;
-(void)viewDidLoad {
-
+ _selectedAssets = [[NSMutableSet alloc] init];
[self.tableView setSeparatorColor:[UIColor clearColor]];
[self.tableView setAllowsSelection:NO];
@@ -28,7 +33,7 @@ -(void)viewDidLoad {
UIBarButtonItem *doneButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneAction:)] autorelease];
[self.navigationItem setRightBarButtonItem:doneButtonItem];
- [self.navigationItem setTitle:@"Loading..."];
+ [self.navigationItem setTitle:NSLocalizedString(@"Loading...", @"Loading...")];
[self performSelectorInBackground:@selector(preparePhotos) withObject:nil];
@@ -52,11 +57,11 @@ -(void)preparePhotos {
ELCAsset *elcAsset = [[[ELCAsset alloc] initWithAsset:result] autorelease];
[elcAsset setParent:self];
[self.elcAssets addObject:elcAsset];
- }];
+ }];
NSLog(@"done enumerating photos");
[self.tableView reloadData];
- [self.navigationItem setTitle:@"Pick Photos"];
+ [self.navigationItem setTitle:NSLocalizedString(@"Pick Photos", @"Pick Photos")];
[pool release];
@@ -135,15 +140,22 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
ELCAssetCell *cell = (ELCAssetCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
+ NSArray *assets = [self assetsForIndexPath:indexPath];
if (cell == nil)
{
- cell = [[[ELCAssetCell alloc] initWithAssets:[self assetsForIndexPath:indexPath] reuseIdentifier:CellIdentifier] autorelease];
+ cell = [[[ELCAssetCell alloc] initWithAssets:assets reuseIdentifier:CellIdentifier] autorelease];
}
else
- {
- [cell setAssets:[self assetsForIndexPath:indexPath]];
+ {
+ [cell setAssets:assets];
}
+ // Show to user that assets of different type than the selected ones
+ // can't be selected.
+ for(ELCAsset *asset in assets) {
+ [asset setDisabled:![self canSelectAsset:asset.asset]];
+ }
+
return cell;
}
@@ -167,6 +179,34 @@ - (int)totalSelectedAssets {
return count;
}
+- (BOOL)canSelectAsset:(ALAsset *)asset
+{
+ ALAsset *lastSelectedAsset = [_selectedAssets anyObject];
+ if (lastSelectedAsset && [lastSelectedAsset valueForProperty:ALAssetPropertyType] != [asset valueForProperty:ALAssetPropertyType]) {
+ return NO;
+ }
+
+ return YES;
+}
+
+- (void)didDeselectAsset:(ALAsset *)asset
+{
+ [_selectedAssets removeObject:asset];
+
+ if ([_selectedAssets count] == 0) {
+ [self.tableView reloadData];
+ }
+}
+
+- (void)didSelectAsset:(ALAsset *)asset
+{
+ BOOL firstSelection = [_selectedAssets count] == 0;
+ [_selectedAssets addObject:asset];
+ if (firstSelection) {
+ [self.tableView reloadData];
+ }
+}
+
- (void)dealloc
{
[elcAssets release];
View
19 Classes/ELCImagePickerController.m
@@ -23,26 +23,11 @@ -(void)cancelImagePicker {
}
-(void)selectedAssets:(NSArray*)_assets {
-
- NSMutableArray *returnArray = [[[NSMutableArray alloc] init] autorelease];
-
- for(ALAsset *asset in _assets) {
-
- NSMutableDictionary *workingDictionary = [[NSMutableDictionary alloc] init];
- [workingDictionary setObject:[asset valueForProperty:ALAssetPropertyType] forKey:@"UIImagePickerControllerMediaType"];
- [workingDictionary setObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]] forKey:@"UIImagePickerControllerOriginalImage"];
- [workingDictionary setObject:[[asset valueForProperty:ALAssetPropertyURLs] valueForKey:[[[asset valueForProperty:ALAssetPropertyURLs] allKeys] objectAtIndex:0]] forKey:@"UIImagePickerControllerReferenceURL"];
-
- [returnArray addObject:workingDictionary];
-
- [workingDictionary release];
- }
-
[self popToRootViewControllerAnimated:NO];
- [[self parentViewController] dismissModalViewControllerAnimated:YES];
+ [[self parentViewController] dismissViewControllerAnimated:YES completion:^{}];
if([delegate respondsToSelector:@selector(elcImagePickerController:didFinishPickingMediaWithInfo:)]) {
- [delegate performSelector:@selector(elcImagePickerController:didFinishPickingMediaWithInfo:) withObject:self withObject:[NSArray arrayWithArray:returnArray]];
+ [delegate performSelector:@selector(elcImagePickerController:didFinishPickingMediaWithInfo:) withObject:self withObject:[NSArray arrayWithArray:_assets]];
}
}
View
4 Classes/ELCImagePickerDemoViewController.m
@@ -32,7 +32,7 @@ -(IBAction)launchController {
- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info {
- [self dismissModalViewControllerAnimated:YES];
+ [self dismissViewControllerAnimated:YES completion:^{}];
for (UIView *v in [scrollview subviews]) {
[v removeFromSuperview];
@@ -59,7 +59,7 @@ - (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPic
- (void)elcImagePickerControllerDidCancel:(ELCImagePickerController *)picker {
- [self dismissModalViewControllerAnimated:YES];
+ [self dismissViewControllerAnimated:YES completion:^{}];
}
- (void)didReceiveMemoryWarning {
Something went wrong with that request. Please try again.