Show loader in QBImagePicker

This commit is contained in:
Andrew Shini 2016-08-30 14:24:09 +10:00
parent 037030ee5b
commit aa041fa038

View File

@ -43,7 +43,7 @@ static CGSize CGSizeScale(CGSize size, CGFloat scale) {
{ {
NSArray *allLayoutAttributes = [self.collectionViewLayout layoutAttributesForElementsInRect:rect]; NSArray *allLayoutAttributes = [self.collectionViewLayout layoutAttributesForElementsInRect:rect];
if (allLayoutAttributes.count == 0) { return nil; } if (allLayoutAttributes.count == 0) { return nil; }
NSMutableArray *indexPaths = [NSMutableArray arrayWithCapacity:allLayoutAttributes.count]; NSMutableArray *indexPaths = [NSMutableArray arrayWithCapacity:allLayoutAttributes.count];
for (UICollectionViewLayoutAttributes *layoutAttributes in allLayoutAttributes) { for (UICollectionViewLayoutAttributes *layoutAttributes in allLayoutAttributes) {
NSIndexPath *indexPath = layoutAttributes.indexPath; NSIndexPath *indexPath = layoutAttributes.indexPath;
@ -73,10 +73,10 @@ static CGSize CGSizeScale(CGSize size, CGFloat scale) {
- (void)viewDidLoad - (void)viewDidLoad
{ {
[super viewDidLoad]; [super viewDidLoad];
[self setUpToolbarItems]; [self setUpToolbarItems];
[self resetCachedAssets]; [self resetCachedAssets];
// Register observer // Register observer
[[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self]; [[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self];
} }
@ -84,25 +84,25 @@ static CGSize CGSizeScale(CGSize size, CGFloat scale) {
- (void)viewWillAppear:(BOOL)animated - (void)viewWillAppear:(BOOL)animated
{ {
[super viewWillAppear:animated]; [super viewWillAppear:animated];
// Configure navigation item // Configure navigation item
self.navigationItem.title = self.assetCollection.localizedTitle; self.navigationItem.title = self.assetCollection.localizedTitle;
self.navigationItem.prompt = self.imagePickerController.prompt; self.navigationItem.prompt = self.imagePickerController.prompt;
// Configure collection view // Configure collection view
self.collectionView.allowsMultipleSelection = self.imagePickerController.allowsMultipleSelection; self.collectionView.allowsMultipleSelection = self.imagePickerController.allowsMultipleSelection;
// Show/hide 'Done' button // Show/hide 'Done' button
if (self.imagePickerController.allowsMultipleSelection) { if (self.imagePickerController.allowsMultipleSelection) {
[self.navigationItem setRightBarButtonItem:self.doneButton animated:NO]; [self.navigationItem setRightBarButtonItem:self.doneButton animated:NO];
} else { } else {
[self.navigationItem setRightBarButtonItem:nil animated:NO]; [self.navigationItem setRightBarButtonItem:nil animated:NO];
} }
[self updateDoneButtonState]; [self updateDoneButtonState];
[self updateSelectionInfo]; [self updateSelectionInfo];
[self.collectionView reloadData]; [self.collectionView reloadData];
// Scroll to bottom // Scroll to bottom
if (self.fetchResult.count > 0 && self.isMovingToParentViewController && !self.disableScrollToBottom) { if (self.fetchResult.count > 0 && self.isMovingToParentViewController && !self.disableScrollToBottom) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:(self.fetchResult.count - 1) inSection:0]; NSIndexPath *indexPath = [NSIndexPath indexPathForItem:(self.fetchResult.count - 1) inSection:0];
@ -113,16 +113,16 @@ static CGSize CGSizeScale(CGSize size, CGFloat scale) {
- (void)viewWillDisappear:(BOOL)animated - (void)viewWillDisappear:(BOOL)animated
{ {
[super viewWillDisappear:animated]; [super viewWillDisappear:animated];
self.disableScrollToBottom = YES; self.disableScrollToBottom = YES;
} }
- (void)viewDidAppear:(BOOL)animated - (void)viewDidAppear:(BOOL)animated
{ {
[super viewDidAppear:animated]; [super viewDidAppear:animated];
self.disableScrollToBottom = NO; self.disableScrollToBottom = NO;
[self updateCachedAssets]; [self updateCachedAssets];
} }
@ -130,10 +130,10 @@ static CGSize CGSizeScale(CGSize size, CGFloat scale) {
{ {
// Save indexPath for the last item // Save indexPath for the last item
NSIndexPath *indexPath = [[self.collectionView indexPathsForVisibleItems] lastObject]; NSIndexPath *indexPath = [[self.collectionView indexPathsForVisibleItems] lastObject];
// Update layout // Update layout
[self.collectionViewLayout invalidateLayout]; [self.collectionViewLayout invalidateLayout];
// Restore scroll position // Restore scroll position
[coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) { [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionBottom animated:NO]; [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionBottom animated:NO];
@ -152,7 +152,7 @@ static CGSize CGSizeScale(CGSize size, CGFloat scale) {
- (void)setAssetCollection:(PHAssetCollection *)assetCollection - (void)setAssetCollection:(PHAssetCollection *)assetCollection
{ {
_assetCollection = assetCollection; _assetCollection = assetCollection;
[self updateFetchRequest]; [self updateFetchRequest];
[self.collectionView reloadData]; [self.collectionView reloadData];
} }
@ -162,7 +162,7 @@ static CGSize CGSizeScale(CGSize size, CGFloat scale) {
if (_imageManager == nil) { if (_imageManager == nil) {
_imageManager = [PHCachingImageManager new]; _imageManager = [PHCachingImageManager new];
} }
return _imageManager; return _imageManager;
} }
@ -177,12 +177,22 @@ static CGSize CGSizeScale(CGSize size, CGFloat scale) {
- (IBAction)done:(id)sender - (IBAction)done:(id)sender
{ {
[NSThread detachNewThreadSelector:@selector(showLoader) toTarget:self withObject:nil];
if ([self.imagePickerController.delegate respondsToSelector:@selector(qb_imagePickerController:didFinishPickingAssets:)]) { if ([self.imagePickerController.delegate respondsToSelector:@selector(qb_imagePickerController:didFinishPickingAssets:)]) {
[self.imagePickerController.delegate qb_imagePickerController:self.imagePickerController [self.imagePickerController.delegate qb_imagePickerController:self.imagePickerController
didFinishPickingAssets:self.imagePickerController.selectedAssets.array]; didFinishPickingAssets:self.imagePickerController.selectedAssets.array];
} }
} }
- (void) showLoader {
UIActivityIndicatorView * activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
UIBarButtonItem * loadingView = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
activityIndicator.color = [UIColor blackColor];
[self.navigationItem setRightBarButtonItem:loadingView animated:NO];
[activityIndicator startAnimating];
}
#pragma mark - Toolbar #pragma mark - Toolbar
@ -191,21 +201,21 @@ static CGSize CGSizeScale(CGSize size, CGFloat scale) {
// Space // Space
UIBarButtonItem *leftSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL]; UIBarButtonItem *leftSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
UIBarButtonItem *rightSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL]; UIBarButtonItem *rightSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
// Info label // Info label
NSDictionary *attributes = @{ NSForegroundColorAttributeName: [UIColor blackColor] }; NSDictionary *attributes = @{ NSForegroundColorAttributeName: [UIColor blackColor] };
UIBarButtonItem *infoButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:NULL]; UIBarButtonItem *infoButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:NULL];
infoButtonItem.enabled = NO; infoButtonItem.enabled = NO;
[infoButtonItem setTitleTextAttributes:attributes forState:UIControlStateNormal]; [infoButtonItem setTitleTextAttributes:attributes forState:UIControlStateNormal];
[infoButtonItem setTitleTextAttributes:attributes forState:UIControlStateDisabled]; [infoButtonItem setTitleTextAttributes:attributes forState:UIControlStateDisabled];
self.toolbarItems = @[leftSpace, infoButtonItem, rightSpace]; self.toolbarItems = @[leftSpace, infoButtonItem, rightSpace];
} }
- (void)updateSelectionInfo - (void)updateSelectionInfo
{ {
NSMutableOrderedSet *selectedAssets = self.imagePickerController.selectedAssets; NSMutableOrderedSet *selectedAssets = self.imagePickerController.selectedAssets;
if (selectedAssets.count > 0) { if (selectedAssets.count > 0) {
NSBundle *bundle = self.imagePickerController.assetBundle; NSBundle *bundle = self.imagePickerController.assetBundle;
NSString *format; NSString *format;
@ -214,7 +224,7 @@ static CGSize CGSizeScale(CGSize size, CGFloat scale) {
} else { } else {
format = NSLocalizedStringFromTableInBundle(@"assets.toolbar.item-selected", @"QBImagePicker", bundle, nil); format = NSLocalizedStringFromTableInBundle(@"assets.toolbar.item-selected", @"QBImagePicker", bundle, nil);
} }
NSString *title = [NSString stringWithFormat:format, selectedAssets.count]; NSString *title = [NSString stringWithFormat:format, selectedAssets.count];
[(UIBarButtonItem *)self.toolbarItems[1] setTitle:title]; [(UIBarButtonItem *)self.toolbarItems[1] setTitle:title];
} else { } else {
@ -229,22 +239,22 @@ static CGSize CGSizeScale(CGSize size, CGFloat scale) {
{ {
if (self.assetCollection) { if (self.assetCollection) {
PHFetchOptions *options = [PHFetchOptions new]; PHFetchOptions *options = [PHFetchOptions new];
switch (self.imagePickerController.mediaType) { switch (self.imagePickerController.mediaType) {
case QBImagePickerMediaTypeImage: case QBImagePickerMediaTypeImage:
options.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeImage]; options.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeImage];
break; break;
case QBImagePickerMediaTypeVideo: case QBImagePickerMediaTypeVideo:
options.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeVideo]; options.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeVideo];
break; break;
default: default:
break; break;
} }
self.fetchResult = [PHAsset fetchAssetsInAssetCollection:self.assetCollection options:options]; self.fetchResult = [PHAsset fetchAssetsInAssetCollection:self.assetCollection options:options];
if ([self isAutoDeselectEnabled] && self.imagePickerController.selectedAssets.count > 0) { if ([self isAutoDeselectEnabled] && self.imagePickerController.selectedAssets.count > 0) {
// Get index of previous selected asset // Get index of previous selected asset
PHAsset *asset = [self.imagePickerController.selectedAssets firstObject]; PHAsset *asset = [self.imagePickerController.selectedAssets firstObject];
@ -267,11 +277,11 @@ static CGSize CGSizeScale(CGSize size, CGFloat scale) {
- (BOOL)isMaximumSelectionLimitReached - (BOOL)isMaximumSelectionLimitReached
{ {
NSUInteger minimumNumberOfSelection = MAX(1, self.imagePickerController.minimumNumberOfSelection); NSUInteger minimumNumberOfSelection = MAX(1, self.imagePickerController.minimumNumberOfSelection);
if (minimumNumberOfSelection <= self.imagePickerController.maximumNumberOfSelection) { if (minimumNumberOfSelection <= self.imagePickerController.maximumNumberOfSelection) {
return (self.imagePickerController.maximumNumberOfSelection <= self.imagePickerController.selectedAssets.count); return (self.imagePickerController.maximumNumberOfSelection <= self.imagePickerController.selectedAssets.count);
} }
return NO; return NO;
} }
@ -293,19 +303,19 @@ static CGSize CGSizeScale(CGSize size, CGFloat scale) {
{ {
BOOL isViewVisible = [self isViewLoaded] && self.view.window != nil; BOOL isViewVisible = [self isViewLoaded] && self.view.window != nil;
if (!isViewVisible) { return; } if (!isViewVisible) { return; }
// The preheat window is twice the height of the visible rect // The preheat window is twice the height of the visible rect
CGRect preheatRect = self.collectionView.bounds; CGRect preheatRect = self.collectionView.bounds;
preheatRect = CGRectInset(preheatRect, 0.0, -0.5 * CGRectGetHeight(preheatRect)); preheatRect = CGRectInset(preheatRect, 0.0, -0.5 * CGRectGetHeight(preheatRect));
// If scrolled by a "reasonable" amount... // If scrolled by a "reasonable" amount...
CGFloat delta = ABS(CGRectGetMidY(preheatRect) - CGRectGetMidY(self.previousPreheatRect)); CGFloat delta = ABS(CGRectGetMidY(preheatRect) - CGRectGetMidY(self.previousPreheatRect));
if (delta > CGRectGetHeight(self.collectionView.bounds) / 3.0) { if (delta > CGRectGetHeight(self.collectionView.bounds) / 3.0) {
// Compute the assets to start caching and to stop caching // Compute the assets to start caching and to stop caching
NSMutableArray *addedIndexPaths = [NSMutableArray array]; NSMutableArray *addedIndexPaths = [NSMutableArray array];
NSMutableArray *removedIndexPaths = [NSMutableArray array]; NSMutableArray *removedIndexPaths = [NSMutableArray array];
[self computeDifferenceBetweenRect:self.previousPreheatRect andRect:preheatRect addedHandler:^(CGRect addedRect) { [self computeDifferenceBetweenRect:self.previousPreheatRect andRect:preheatRect addedHandler:^(CGRect addedRect) {
NSArray *indexPaths = [self.collectionView qb_indexPathsForElementsInRect:addedRect]; NSArray *indexPaths = [self.collectionView qb_indexPathsForElementsInRect:addedRect];
[addedIndexPaths addObjectsFromArray:indexPaths]; [addedIndexPaths addObjectsFromArray:indexPaths];
@ -313,13 +323,13 @@ static CGSize CGSizeScale(CGSize size, CGFloat scale) {
NSArray *indexPaths = [self.collectionView qb_indexPathsForElementsInRect:removedRect]; NSArray *indexPaths = [self.collectionView qb_indexPathsForElementsInRect:removedRect];
[removedIndexPaths addObjectsFromArray:indexPaths]; [removedIndexPaths addObjectsFromArray:indexPaths];
}]; }];
NSArray *assetsToStartCaching = [self assetsAtIndexPaths:addedIndexPaths]; NSArray *assetsToStartCaching = [self assetsAtIndexPaths:addedIndexPaths];
NSArray *assetsToStopCaching = [self assetsAtIndexPaths:removedIndexPaths]; NSArray *assetsToStopCaching = [self assetsAtIndexPaths:removedIndexPaths];
CGSize itemSize = [(UICollectionViewFlowLayout *)self.collectionViewLayout itemSize]; CGSize itemSize = [(UICollectionViewFlowLayout *)self.collectionViewLayout itemSize];
CGSize targetSize = CGSizeScale(itemSize, [[UIScreen mainScreen] scale]); CGSize targetSize = CGSizeScale(itemSize, [[UIScreen mainScreen] scale]);
[self.imageManager startCachingImagesForAssets:assetsToStartCaching [self.imageManager startCachingImagesForAssets:assetsToStartCaching
targetSize:targetSize targetSize:targetSize
contentMode:PHImageContentModeAspectFill contentMode:PHImageContentModeAspectFill
@ -328,7 +338,7 @@ static CGSize CGSizeScale(CGSize size, CGFloat scale) {
targetSize:targetSize targetSize:targetSize
contentMode:PHImageContentModeAspectFill contentMode:PHImageContentModeAspectFill
options:nil]; options:nil];
self.previousPreheatRect = preheatRect; self.previousPreheatRect = preheatRect;
} }
} }
@ -340,7 +350,7 @@ static CGSize CGSizeScale(CGSize size, CGFloat scale) {
CGFloat oldMinY = CGRectGetMinY(oldRect); CGFloat oldMinY = CGRectGetMinY(oldRect);
CGFloat newMaxY = CGRectGetMaxY(newRect); CGFloat newMaxY = CGRectGetMaxY(newRect);
CGFloat newMinY = CGRectGetMinY(newRect); CGFloat newMinY = CGRectGetMinY(newRect);
if (newMaxY > oldMaxY) { if (newMaxY > oldMaxY) {
CGRect rectToAdd = CGRectMake(newRect.origin.x, oldMaxY, newRect.size.width, (newMaxY - oldMaxY)); CGRect rectToAdd = CGRectMake(newRect.origin.x, oldMaxY, newRect.size.width, (newMaxY - oldMaxY));
addedHandler(rectToAdd); addedHandler(rectToAdd);
@ -366,7 +376,7 @@ static CGSize CGSizeScale(CGSize size, CGFloat scale) {
- (NSArray *)assetsAtIndexPaths:(NSArray *)indexPaths - (NSArray *)assetsAtIndexPaths:(NSArray *)indexPaths
{ {
if (indexPaths.count == 0) { return nil; } if (indexPaths.count == 0) { return nil; }
NSMutableArray *assets = [NSMutableArray arrayWithCapacity:indexPaths.count]; NSMutableArray *assets = [NSMutableArray arrayWithCapacity:indexPaths.count];
for (NSIndexPath *indexPath in indexPaths) { for (NSIndexPath *indexPath in indexPaths) {
if (indexPath.item < self.fetchResult.count) { if (indexPath.item < self.fetchResult.count) {
@ -384,11 +394,11 @@ static CGSize CGSizeScale(CGSize size, CGFloat scale) {
{ {
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
PHFetchResultChangeDetails *collectionChanges = [changeInstance changeDetailsForFetchResult:self.fetchResult]; PHFetchResultChangeDetails *collectionChanges = [changeInstance changeDetailsForFetchResult:self.fetchResult];
if (collectionChanges) { if (collectionChanges) {
// Get the new fetch result // Get the new fetch result
self.fetchResult = [collectionChanges fetchResultAfterChanges]; self.fetchResult = [collectionChanges fetchResultAfterChanges];
if (![collectionChanges hasIncrementalChanges] || [collectionChanges hasMoves]) { if (![collectionChanges hasIncrementalChanges] || [collectionChanges hasMoves]) {
// We need to reload all if the incremental diffs are not available // We need to reload all if the incremental diffs are not available
[self.collectionView reloadData]; [self.collectionView reloadData];
@ -399,19 +409,19 @@ static CGSize CGSizeScale(CGSize size, CGFloat scale) {
if ([removedIndexes count]) { if ([removedIndexes count]) {
[self.collectionView deleteItemsAtIndexPaths:[removedIndexes qb_indexPathsFromIndexesWithSection:0]]; [self.collectionView deleteItemsAtIndexPaths:[removedIndexes qb_indexPathsFromIndexesWithSection:0]];
} }
NSIndexSet *insertedIndexes = [collectionChanges insertedIndexes]; NSIndexSet *insertedIndexes = [collectionChanges insertedIndexes];
if ([insertedIndexes count]) { if ([insertedIndexes count]) {
[self.collectionView insertItemsAtIndexPaths:[insertedIndexes qb_indexPathsFromIndexesWithSection:0]]; [self.collectionView insertItemsAtIndexPaths:[insertedIndexes qb_indexPathsFromIndexesWithSection:0]];
} }
NSIndexSet *changedIndexes = [collectionChanges changedIndexes]; NSIndexSet *changedIndexes = [collectionChanges changedIndexes];
if ([changedIndexes count]) { if ([changedIndexes count]) {
[self.collectionView reloadItemsAtIndexPaths:[changedIndexes qb_indexPathsFromIndexesWithSection:0]]; [self.collectionView reloadItemsAtIndexPaths:[changedIndexes qb_indexPathsFromIndexesWithSection:0]];
} }
} completion:NULL]; } completion:NULL];
} }
[self resetCachedAssets]; [self resetCachedAssets];
} }
}); });
@ -443,12 +453,12 @@ static CGSize CGSizeScale(CGSize size, CGFloat scale) {
QBAssetCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AssetCell" forIndexPath:indexPath]; QBAssetCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AssetCell" forIndexPath:indexPath];
cell.tag = indexPath.item; cell.tag = indexPath.item;
cell.showsOverlayViewWhenSelected = self.imagePickerController.allowsMultipleSelection; cell.showsOverlayViewWhenSelected = self.imagePickerController.allowsMultipleSelection;
// Image // Image
PHAsset *asset = self.fetchResult[indexPath.item]; PHAsset *asset = self.fetchResult[indexPath.item];
CGSize itemSize = [(UICollectionViewFlowLayout *)collectionView.collectionViewLayout itemSize]; CGSize itemSize = [(UICollectionViewFlowLayout *)collectionView.collectionViewLayout itemSize];
CGSize targetSize = CGSizeScale(itemSize, [[UIScreen mainScreen] scale]); CGSize targetSize = CGSizeScale(itemSize, [[UIScreen mainScreen] scale]);
[self.imageManager requestImageForAsset:asset [self.imageManager requestImageForAsset:asset
targetSize:targetSize targetSize:targetSize
contentMode:PHImageContentModeAspectFill contentMode:PHImageContentModeAspectFill
@ -458,15 +468,15 @@ static CGSize CGSizeScale(CGSize size, CGFloat scale) {
cell.imageView.image = result; cell.imageView.image = result;
} }
}]; }];
// Video indicator // Video indicator
if (asset.mediaType == PHAssetMediaTypeVideo) { if (asset.mediaType == PHAssetMediaTypeVideo) {
cell.videoIndicatorView.hidden = NO; cell.videoIndicatorView.hidden = NO;
NSInteger minutes = (NSInteger)(asset.duration / 60.0); NSInteger minutes = (NSInteger)(asset.duration / 60.0);
NSInteger seconds = (NSInteger)ceil(asset.duration - 60.0 * (double)minutes); NSInteger seconds = (NSInteger)ceil(asset.duration - 60.0 * (double)minutes);
cell.videoIndicatorView.timeLabel.text = [NSString stringWithFormat:@"%02ld:%02ld", (long)minutes, (long)seconds]; cell.videoIndicatorView.timeLabel.text = [NSString stringWithFormat:@"%02ld:%02ld", (long)minutes, (long)seconds];
if (asset.mediaSubtypes & PHAssetMediaSubtypeVideoHighFrameRate) { if (asset.mediaSubtypes & PHAssetMediaSubtypeVideoHighFrameRate) {
cell.videoIndicatorView.videoIcon.hidden = YES; cell.videoIndicatorView.videoIcon.hidden = YES;
cell.videoIndicatorView.slomoIcon.hidden = NO; cell.videoIndicatorView.slomoIcon.hidden = NO;
@ -478,13 +488,13 @@ static CGSize CGSizeScale(CGSize size, CGFloat scale) {
} else { } else {
cell.videoIndicatorView.hidden = YES; cell.videoIndicatorView.hidden = YES;
} }
// Selection state // Selection state
if ([self.imagePickerController.selectedAssets containsObject:asset]) { if ([self.imagePickerController.selectedAssets containsObject:asset]) {
[cell setSelected:YES]; [cell setSelected:YES];
[collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone]; [collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
} }
return cell; return cell;
} }
@ -494,14 +504,14 @@ static CGSize CGSizeScale(CGSize size, CGFloat scale) {
UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter
withReuseIdentifier:@"FooterView" withReuseIdentifier:@"FooterView"
forIndexPath:indexPath]; forIndexPath:indexPath];
// Number of assets // Number of assets
UILabel *label = (UILabel *)[footerView viewWithTag:1]; UILabel *label = (UILabel *)[footerView viewWithTag:1];
NSBundle *bundle = self.imagePickerController.assetBundle; NSBundle *bundle = self.imagePickerController.assetBundle;
NSUInteger numberOfPhotos = [self.fetchResult countOfAssetsWithMediaType:PHAssetMediaTypeImage]; NSUInteger numberOfPhotos = [self.fetchResult countOfAssetsWithMediaType:PHAssetMediaTypeImage];
NSUInteger numberOfVideos = [self.fetchResult countOfAssetsWithMediaType:PHAssetMediaTypeVideo]; NSUInteger numberOfVideos = [self.fetchResult countOfAssetsWithMediaType:PHAssetMediaTypeVideo];
switch (self.imagePickerController.mediaType) { switch (self.imagePickerController.mediaType) {
case QBImagePickerMediaTypeAny: case QBImagePickerMediaTypeAny:
{ {
@ -517,33 +527,33 @@ static CGSize CGSizeScale(CGSize size, CGFloat scale) {
} else { } else {
format = NSLocalizedStringFromTableInBundle(@"assets.footer.photos-and-videos", @"QBImagePicker", bundle, nil); format = NSLocalizedStringFromTableInBundle(@"assets.footer.photos-and-videos", @"QBImagePicker", bundle, nil);
} }
label.text = [NSString stringWithFormat:format, numberOfPhotos, numberOfVideos]; label.text = [NSString stringWithFormat:format, numberOfPhotos, numberOfVideos];
} }
break; break;
case QBImagePickerMediaTypeImage: case QBImagePickerMediaTypeImage:
{ {
NSString *key = (numberOfPhotos == 1) ? @"assets.footer.photo" : @"assets.footer.photos"; NSString *key = (numberOfPhotos == 1) ? @"assets.footer.photo" : @"assets.footer.photos";
NSString *format = NSLocalizedStringFromTableInBundle(key, @"QBImagePicker", bundle, nil); NSString *format = NSLocalizedStringFromTableInBundle(key, @"QBImagePicker", bundle, nil);
label.text = [NSString stringWithFormat:format, numberOfPhotos]; label.text = [NSString stringWithFormat:format, numberOfPhotos];
} }
break; break;
case QBImagePickerMediaTypeVideo: case QBImagePickerMediaTypeVideo:
{ {
NSString *key = (numberOfVideos == 1) ? @"assets.footer.video" : @"assets.footer.videos"; NSString *key = (numberOfVideos == 1) ? @"assets.footer.video" : @"assets.footer.videos";
NSString *format = NSLocalizedStringFromTableInBundle(key, @"QBImagePicker", bundle, nil); NSString *format = NSLocalizedStringFromTableInBundle(key, @"QBImagePicker", bundle, nil);
label.text = [NSString stringWithFormat:format, numberOfVideos]; label.text = [NSString stringWithFormat:format, numberOfVideos];
} }
break; break;
} }
return footerView; return footerView;
} }
return nil; return nil;
} }
@ -556,11 +566,11 @@ static CGSize CGSizeScale(CGSize size, CGFloat scale) {
PHAsset *asset = self.fetchResult[indexPath.item]; PHAsset *asset = self.fetchResult[indexPath.item];
return [self.imagePickerController.delegate qb_imagePickerController:self.imagePickerController shouldSelectAsset:asset]; return [self.imagePickerController.delegate qb_imagePickerController:self.imagePickerController shouldSelectAsset:asset];
} }
if ([self isAutoDeselectEnabled]) { if ([self isAutoDeselectEnabled]) {
return YES; return YES;
} }
return ![self isMaximumSelectionLimitReached]; return ![self isMaximumSelectionLimitReached];
} }
@ -568,30 +578,30 @@ static CGSize CGSizeScale(CGSize size, CGFloat scale) {
{ {
QBImagePickerController *imagePickerController = self.imagePickerController; QBImagePickerController *imagePickerController = self.imagePickerController;
NSMutableOrderedSet *selectedAssets = imagePickerController.selectedAssets; NSMutableOrderedSet *selectedAssets = imagePickerController.selectedAssets;
PHAsset *asset = self.fetchResult[indexPath.item]; PHAsset *asset = self.fetchResult[indexPath.item];
if (imagePickerController.allowsMultipleSelection) { if (imagePickerController.allowsMultipleSelection) {
if ([self isAutoDeselectEnabled] && selectedAssets.count > 0) { if ([self isAutoDeselectEnabled] && selectedAssets.count > 0) {
// Remove previous selected asset from set // Remove previous selected asset from set
[selectedAssets removeObjectAtIndex:0]; [selectedAssets removeObjectAtIndex:0];
// Deselect previous selected asset // Deselect previous selected asset
if (self.lastSelectedItemIndexPath) { if (self.lastSelectedItemIndexPath) {
[collectionView deselectItemAtIndexPath:self.lastSelectedItemIndexPath animated:NO]; [collectionView deselectItemAtIndexPath:self.lastSelectedItemIndexPath animated:NO];
} }
} }
// Add asset to set // Add asset to set
[selectedAssets addObject:asset]; [selectedAssets addObject:asset];
self.lastSelectedItemIndexPath = indexPath; self.lastSelectedItemIndexPath = indexPath;
[self updateDoneButtonState]; [self updateDoneButtonState];
if (imagePickerController.showsNumberOfSelectedAssets) { if (imagePickerController.showsNumberOfSelectedAssets) {
[self updateSelectionInfo]; [self updateSelectionInfo];
if (selectedAssets.count == 1) { if (selectedAssets.count == 1) {
// Show toolbar // Show toolbar
[self.navigationController setToolbarHidden:NO animated:YES]; [self.navigationController setToolbarHidden:NO animated:YES];
@ -602,7 +612,7 @@ static CGSize CGSizeScale(CGSize size, CGFloat scale) {
[imagePickerController.delegate qb_imagePickerController:imagePickerController didFinishPickingAssets:@[asset]]; [imagePickerController.delegate qb_imagePickerController:imagePickerController didFinishPickingAssets:@[asset]];
} }
} }
if ([imagePickerController.delegate respondsToSelector:@selector(qb_imagePickerController:didSelectAsset:)]) { if ([imagePickerController.delegate respondsToSelector:@selector(qb_imagePickerController:didSelectAsset:)]) {
[imagePickerController.delegate qb_imagePickerController:imagePickerController didSelectAsset:asset]; [imagePickerController.delegate qb_imagePickerController:imagePickerController didSelectAsset:asset];
} }
@ -613,28 +623,28 @@ static CGSize CGSizeScale(CGSize size, CGFloat scale) {
if (!self.imagePickerController.allowsMultipleSelection) { if (!self.imagePickerController.allowsMultipleSelection) {
return; return;
} }
QBImagePickerController *imagePickerController = self.imagePickerController; QBImagePickerController *imagePickerController = self.imagePickerController;
NSMutableOrderedSet *selectedAssets = imagePickerController.selectedAssets; NSMutableOrderedSet *selectedAssets = imagePickerController.selectedAssets;
PHAsset *asset = self.fetchResult[indexPath.item]; PHAsset *asset = self.fetchResult[indexPath.item];
// Remove asset from set // Remove asset from set
[selectedAssets removeObject:asset]; [selectedAssets removeObject:asset];
self.lastSelectedItemIndexPath = nil; self.lastSelectedItemIndexPath = nil;
[self updateDoneButtonState]; [self updateDoneButtonState];
if (imagePickerController.showsNumberOfSelectedAssets) { if (imagePickerController.showsNumberOfSelectedAssets) {
[self updateSelectionInfo]; [self updateSelectionInfo];
if (selectedAssets.count == 0) { if (selectedAssets.count == 0) {
// Hide toolbar // Hide toolbar
[self.navigationController setToolbarHidden:YES animated:YES]; [self.navigationController setToolbarHidden:YES animated:YES];
} }
} }
if ([imagePickerController.delegate respondsToSelector:@selector(qb_imagePickerController:didDeselectAsset:)]) { if ([imagePickerController.delegate respondsToSelector:@selector(qb_imagePickerController:didDeselectAsset:)]) {
[imagePickerController.delegate qb_imagePickerController:imagePickerController didDeselectAsset:asset]; [imagePickerController.delegate qb_imagePickerController:imagePickerController didDeselectAsset:asset];
} }
@ -651,9 +661,9 @@ static CGSize CGSizeScale(CGSize size, CGFloat scale) {
} else { } else {
numberOfColumns = self.imagePickerController.numberOfColumnsInLandscape; numberOfColumns = self.imagePickerController.numberOfColumnsInLandscape;
} }
CGFloat width = (CGRectGetWidth(self.view.frame) - 2.0 * (numberOfColumns - 1)) / numberOfColumns; CGFloat width = (CGRectGetWidth(self.view.frame) - 2.0 * (numberOfColumns - 1)) / numberOfColumns;
return CGSizeMake(width, width); return CGSizeMake(width, width);
} }