Skip to content

Commit

Permalink
Merge pull request guillermomuntaner#14 from albertbori/master
Browse files Browse the repository at this point in the history
I haven't properly tested all this but just from code it looks good. If any problem occurs just let me know. Thanks for the work and the nice features added!
  • Loading branch information
guillermomuntaner committed Aug 10, 2015
2 parents 248af20 + ee0f1b8 commit 6318b40
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 36 deletions.
2 changes: 1 addition & 1 deletion GMImagePicker/GMAlbumsViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reus
_videoIcon = [UIImageView new];
_videoIcon.contentMode = UIViewContentModeScaleAspectFill;
_videoIcon.frame = CGRectMake(3,kAlbumThumbnailSize1.height - 4 - 8, 15, 8 );
_videoIcon.image = [UIImage imageNamed:@"GMVideoIcon"];
_videoIcon.image = [UIImage imageNamed:@"GMVideoIcon" inBundle:[NSBundle bundleForClass:GMAlbumsViewCell.class] compatibleWithTraitCollection:nil];
_videoIcon.clipsToBounds = YES;
_videoIcon.translatesAutoresizingMaskIntoConstraints = YES;
_videoIcon.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
Expand Down
35 changes: 14 additions & 21 deletions GMImagePicker/GMAlbumsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,44 +61,34 @@ - (void)viewDidLoad
//if (self.picker.showsCancelButton)
{
self.navigationItem.leftBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:NSLocalizedStringFromTable(@"picker.navigation.cancel-button", @"GMImagePicker",@"Cancel")
[[UIBarButtonItem alloc] initWithTitle:NSLocalizedStringFromTableInBundle(@"picker.navigation.cancel-button", @"GMImagePicker", [NSBundle bundleForClass:GMImagePickerController.class], @"Cancel")
style:UIBarButtonItemStylePlain
target:self.picker
action:@selector(dismiss:)];
}

self.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:NSLocalizedStringFromTable(@"picker.navigation.done-button", @"GMImagePicker",@"Done")
[[UIBarButtonItem alloc] initWithTitle:NSLocalizedStringFromTableInBundle(@"picker.navigation.done-button", @"GMImagePicker", [NSBundle bundleForClass:GMImagePickerController.class], @"Done")
style:UIBarButtonItemStyleDone
target:self.picker
action:@selector(finishPickingAssets:)];

self.navigationItem.rightBarButtonItem.enabled = (self.picker.selectedAssets.count > 0);
self.navigationItem.rightBarButtonItem.enabled = (self.picker.autoDisableDoneButton ? self.picker.selectedAssets.count > 0 : TRUE);

//Bottom toolbar
self.toolbarItems = self.picker.toolbarItems;

//Title
if (!self.picker.title)
self.title = NSLocalizedStringFromTable(@"picker.navigation.title", @"GMImagePicker",@"Navigation bar default title");
self.title = NSLocalizedStringFromTableInBundle(@"picker.navigation.title", @"GMImagePicker", [NSBundle bundleForClass:GMImagePickerController.class], @"Navigation bar default title");
else
self.title = self.picker.title;


// TO-DO Customizable predicates:
// Predicate has to filter properties of the type of object returned by the PHFetchResult:
// PHCollectionList, PHAssetCollection and PHAsset require different predicates
// with limited posibilities (cannot filter a collection by mediaType for example)

//NSPredicate *predicatePHCollectionList = [NSPredicate predicateWithFormat:@"(mediaType == %d)", PHAssetMediaTypeImage];
//NSPredicate *predicatePHAssetCollection = [NSPredicate predicateWithFormat:@"(mediaType == %d)", PHAssetMediaTypeImage];
//NSPredicate *predicatePHAsset = [NSPredicate predicateWithFormat:@"(mediaType == %d)", PHAssetMediaTypeImage];

//Fetch PHAssetCollections:
PHFetchResult *topLevelUserCollections = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil];
PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
self.collectionsFetchResults = @[topLevelUserCollections, smartAlbums];
self.collectionsLocalizedTitles = @[NSLocalizedStringFromTable(@"picker.table.user-albums-header", @"GMImagePicker",@"Albums"), NSLocalizedStringFromTable(@"picker.table.smart-albums-header", @"GMImagePicker",@"Smart Albums")];
self.collectionsLocalizedTitles = @[NSLocalizedStringFromTableInBundle(@"picker.table.user-albums-header", @"GMImagePicker", [NSBundle bundleForClass:GMImagePickerController.class], @"Albums"), NSLocalizedStringFromTableInBundle(@"picker.table.smart-albums-header", @"GMImagePicker", [NSBundle bundleForClass:GMImagePickerController.class], @"Smart Albums")];

[self updateFetchResults];

Expand Down Expand Up @@ -129,11 +119,11 @@ -(void)updateFetchResults
NSMutableArray *allFetchResultLabel = [[NSMutableArray alloc] init];
{
PHFetchOptions *options = [[PHFetchOptions alloc] init];
options.predicate = [NSPredicate predicateWithFormat:@"mediaType in %@", self.picker.mediaTypes];
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
//options.predicate = predicatePHAsset;
PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsWithOptions:options];
[allFetchResultArray addObject:assetsFetchResult];
[allFetchResultLabel addObject:NSLocalizedStringFromTable(@"picker.table.all-photos-label", @"GMImagePicker",@"All photos")];
[allFetchResultLabel addObject:NSLocalizedStringFromTableInBundle(@"picker.table.all-photos-label", @"GMImagePicker", [NSBundle bundleForClass:GMImagePickerController.class], @"All photos")];
}

//User albums:
Expand All @@ -143,13 +133,13 @@ -(void)updateFetchResults
{
if ([collection isKindOfClass:[PHAssetCollection class]])
{
//PHFetchOptions *options = [[PHFetchOptions alloc] init];
//options.predicate = predicatePHAsset;
PHFetchOptions *options = [[PHFetchOptions alloc] init];
options.predicate = [NSPredicate predicateWithFormat:@"mediaType in %@", self.picker.mediaTypes];
PHAssetCollection *assetCollection = (PHAssetCollection *)collection;

//Albums collections are allways PHAssetCollectionType=1 & PHAssetCollectionSubtype=2

PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:nil];
PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:options];
[userFetchResultArray addObject:assetsFetchResult];
[userFetchResultLabel addObject:collection.localizedTitle];
}
Expand All @@ -169,8 +159,9 @@ -(void)updateFetchResults
if(self.picker.customSmartCollections && [self.picker.customSmartCollections containsObject:@(assetCollection.assetCollectionSubtype)])
{
PHFetchOptions *options = [[PHFetchOptions alloc] init];
options.predicate = [NSPredicate predicateWithFormat:@"mediaType in %@", self.picker.mediaTypes];
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
//options.predicate = predicatePHAsset;

PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:options];
if(assetsFetchResult.count>0)
{
Expand All @@ -184,6 +175,8 @@ -(void)updateFetchResults
self.collectionsFetchResultsAssets= @[allFetchResultArray,userFetchResultArray,smartFetchResultArray];
self.collectionsFetchResultsTitles= @[allFetchResultLabel,userFetchResultLabel,smartFetchResultLabel];
}


#pragma mark - Accessors

- (GMImagePickerController *)picker
Expand Down
5 changes: 2 additions & 3 deletions GMImagePicker/GMGridViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ - (id)initWithFrame:(CGRect)frame
_videoIcon = [UIImageView new];
_videoIcon.frame = CGRectMake(x_offset, self.bounds.size.height-titleHeight, self.bounds.size.width-2*x_offset, titleHeight);
_videoIcon.contentMode = UIViewContentModeLeft;
_videoIcon.image = [UIImage imageNamed:@"GMVideoIcon"];
_videoIcon.image = [UIImage imageNamed:@"GMVideoIcon" inBundle:[NSBundle bundleForClass:GMGridViewCell.class] compatibleWithTraitCollection:nil];
_videoIcon.translatesAutoresizingMaskIntoConstraints = NO;
_videoIcon.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
[self addSubview:_videoIcon];
Expand Down Expand Up @@ -118,11 +118,10 @@ - (id)initWithFrame:(CGRect)frame
[_selectedButton setImage:nil forState:UIControlStateNormal];
_selectedButton.translatesAutoresizingMaskIntoConstraints = NO;
_selectedButton.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[_selectedButton setImage:[UIImage imageNamed:@"GMSelected"] forState:UIControlStateSelected];
[_selectedButton setImage:[UIImage imageNamed:@"GMSelected" inBundle:[NSBundle bundleForClass:GMGridViewCell.class] compatibleWithTraitCollection:nil] forState:UIControlStateSelected];
_selectedButton.hidden = NO;
_selectedButton.userInteractionEnabled = NO;
[self addSubview:_selectedButton];

}

return self;
Expand Down
4 changes: 2 additions & 2 deletions GMImagePicker/GMGridViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,12 @@ - (void)setupViews
- (void)setupButtons
{
self.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:NSLocalizedStringFromTable(@"picker.navigation.done-button", @"GMImagePicker",@"Done")
[[UIBarButtonItem alloc] initWithTitle:NSLocalizedStringFromTableInBundle(@"picker.navigation.done-button", @"GMImagePicker", [NSBundle bundleForClass:GMImagePickerController.class], @"Done")
style:UIBarButtonItemStyleDone
target:self.picker
action:@selector(finishPickingAssets:)];

self.navigationItem.rightBarButtonItem.enabled = (self.picker.selectedAssets.count > 0);
self.navigationItem.rightBarButtonItem.enabled = (self.picker.autoDisableDoneButton ? self.picker.selectedAssets.count > 0 : TRUE);
}

- (void)setupToolbar
Expand Down
23 changes: 21 additions & 2 deletions GMImagePicker/GMImagePickerController.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,25 @@ static CGSize const kPopoverContentSize = {480, 720};
/** UI Customizations **/

/**
* Determines whether or not the number of assets is shown in the Album list.
* The number of assets is visible by default.
* Determines which smart collections are displayed (int array of enum: PHAssetCollectionSubtypeSmartAlbum)
* The default smart collections are:
* - Favorites
* - RecentlyAdded
* - Videos
* - SlomoVideos
* - Timelapses
* - Bursts
* - Panoramas
*/
@property (nonatomic, strong) NSArray* customSmartCollections;

/**
* Determines which media types are allowed (int array of enum: PHAssetMediaType)
* This defaults to all media types (view, audio and images)
* This can override customSmartCollections behavior (ie, remove video-only smart collections)
*/
@property (nonatomic, strong) NSArray* mediaTypes;

/**
* If set, it displays a promt in the navigation bar
*/
Expand All @@ -62,6 +76,11 @@ static CGSize const kPopoverContentSize = {480, 720};
*/
@property (nonatomic, assign) BOOL displayAlbumsNumberOfAssets;

/**
* Automatically disables the "Done" button if nothing is selected
*/
@property (nonatomic, assign) BOOL autoDisableDoneButton;

/**
* Grid customizations:
*
Expand Down
19 changes: 12 additions & 7 deletions GMImagePicker/GMImagePickerController.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ - (id)init
//Default values:
_displaySelectionInfoToolbar = YES;
_displayAlbumsNumberOfAssets = YES;
_autoDisableDoneButton = YES;

//Grid configuration:
_colsInPortrait = 3;
Expand All @@ -42,6 +43,11 @@ - (id)init
//If you don't want to show smart collections, just put _customSmartCollections to nil;
//_customSmartCollections=nil;

//Which media types will display
_mediaTypes = @[@(PHAssetMediaTypeAudio),
@(PHAssetMediaTypeVideo),
@(PHAssetMediaTypeImage)];

self.preferredContentSize = kPopoverContentSize;

[self setupNavigationController];
Expand Down Expand Up @@ -107,7 +113,7 @@ - (void)updateDoneButton
{
UINavigationController *nav = (UINavigationController *)self.childViewControllers[0];
for (UIViewController *viewController in nav.viewControllers)
viewController.navigationItem.rightBarButtonItem.enabled = (self.selectedAssets.count > 0);
viewController.navigationItem.rightBarButtonItem.enabled = (self.autoDisableDoneButton ? self.selectedAssets.count > 0 : TRUE);
}

- (void)updateToolbar
Expand Down Expand Up @@ -162,23 +168,23 @@ - (NSString *)toolbarTitle

if (nImages>0 && nVideos>0)
{
return [NSString stringWithFormat:NSLocalizedStringFromTable(@"picker.selection.multiple-items", @"GMImagePicker", @"%@ Items Selected" ), @(nImages+nVideos)];
return [NSString stringWithFormat:NSLocalizedStringFromTableInBundle(@"picker.selection.multiple-items", @"GMImagePicker", [NSBundle bundleForClass:GMImagePickerController.class], @"%@ Items Selected" ), @(nImages+nVideos)];
}
else if (nImages>1)
{
return [NSString stringWithFormat:NSLocalizedStringFromTable(@"picker.selection.multiple-photos", @"GMImagePicker", @"%@ Photos Selected"), @(nImages)];
return [NSString stringWithFormat:NSLocalizedStringFromTableInBundle(@"picker.selection.multiple-photos", @"GMImagePicker", [NSBundle bundleForClass:GMImagePickerController.class], @"%@ Photos Selected"), @(nImages)];
}
else if (nImages==1)
{
return NSLocalizedStringFromTable(@"picker.selection.single-photo", @"GMImagePicker", @"1 Photo Selected" );
return NSLocalizedStringFromTableInBundle(@"picker.selection.single-photo", @"GMImagePicker", [NSBundle bundleForClass:GMImagePickerController.class], @"1 Photo Selected" );
}
else if (nVideos>1)
{
return [NSString stringWithFormat:NSLocalizedStringFromTable(@"picker.selection.multiple-videos", @"GMImagePicker", @"%@ Videos Selected"), @(nVideos)];
return [NSString stringWithFormat:NSLocalizedStringFromTableInBundle(@"picker.selection.multiple-videos", @"GMImagePicker", [NSBundle bundleForClass:GMImagePickerController.class], @"%@ Videos Selected"), @(nVideos)];
}
else if (nVideos==1)
{
return NSLocalizedStringFromTable(@"picker.selection.single-video", @"GMImagePicker", @"1 Video Selected");
return NSLocalizedStringFromTableInBundle(@"picker.selection.single-video", @"GMImagePicker", [NSBundle bundleForClass:GMImagePickerController.class], @"1 Video Selected");
}
else
{
Expand Down Expand Up @@ -220,5 +226,4 @@ - (NSArray *)toolbarItems
}



@end

0 comments on commit 6318b40

Please sign in to comment.