Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Example/DVGAssetPickerController/DVGViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ - (IBAction)pickPhoto:(id)sender
{
DVGAssetPickerViewController *picker = [[DVGAssetPickerViewController alloc] init];
picker.delegate = self;
picker.maxNumberOfAssets = 2;
[self presentViewController:picker animated:YES completion:nil];
}

Expand Down
1 change: 1 addition & 0 deletions Pod/Classes/DVGAssetPickerViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ typedef NS_ENUM(NSUInteger, DVGAssetPickerMenuItem) {
- (void)cancel;
@property (nonatomic, weak) id<DVGAssetPickerDelegate> delegate;
@property (strong, nonatomic) ALAssetsLibrary *assetsLibrary;
@property (nonatomic) NSUInteger maxNumberOfAssets;

@end
29 changes: 29 additions & 0 deletions Pod/Classes/DVGAssetPickerViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
static CGFloat const kCollectionViewExpandedHeight = 308.f;
static CGFloat const kCollectionViewPadding = 4.f;
static NSTimeInterval const kExpandAnimationDuration = 0.3;
static CGFloat const kDefaultDisabledSelectionAlpha = .6;

@interface DVGAssetPickerViewController ()
<UIViewControllerTransitioningDelegate,
Expand Down Expand Up @@ -359,6 +360,15 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView

UIImage *thumbnail = [UIImage imageWithCGImage:[asset aspectRatioThumbnail]];
cell.imageView.image = thumbnail;

if ([self canSelectAnotherAsset] || [self.selectedAssets containsObject:asset])
{
cell.imageView.alpha = 1;
}
else
{
cell.imageView.alpha = kDefaultDisabledSelectionAlpha;
}

return cell;
}
Expand All @@ -373,6 +383,15 @@ - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
DVGAssetPickerCheckmarkView *checkmark = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Checkmark" forIndexPath:indexPath];
checkmark.selected = [self.selectedAssets containsObject:asset];
view = checkmark;

if ([self canSelectAnotherAsset] || [self.selectedAssets containsObject:asset])
{
view.hidden = NO;
}
else
{
view.hidden = YES;
}
}

return view;
Expand All @@ -383,6 +402,8 @@ - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
ALAsset *asset = self.assets[indexPath.row];

if ([self canSelectAnotherAsset] == NO && ![self.selectedAssets containsObject:asset]) return;

if (!self.collectionViewExpanded) {
self.selectedAssets = [NSMutableSet setWithObject:asset];
Expand All @@ -394,6 +415,7 @@ - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPa
DVGAssetPickerCollectionLayout *layout = (id)self.collectionView.collectionViewLayout;
layout.showCheckmarks = self.collectionViewExpanded;
[self.tableView reloadData];
[collectionView reloadData];
}];
}
else {
Expand Down Expand Up @@ -450,4 +472,11 @@ - (UIPresentationController *)presentationControllerForPresentedViewController:(
presentingViewController:presenting];
}

#pragma mark - Helpers

- (BOOL)canSelectAnotherAsset
{
return self.maxNumberOfAssets == 0 || self.selectedAssets.count < self.maxNumberOfAssets;
}

@end