@@ -417,15 +417,31 @@ - (void)photoLibraryDidChange:(PHChange *)changeInstance
417
417
if ([insertedIndexes count ]) {
418
418
[self .collectionView insertItemsAtIndexPaths: [insertedIndexes qb_indexPathsFromIndexesWithSection: 0 ]];
419
419
}
420
-
421
- NSIndexSet *changedIndexes = [collectionChanges changedIndexes ];
422
- if ([changedIndexes count ]) {
423
- [self .collectionView reloadItemsAtIndexPaths: [changedIndexes qb_indexPathsFromIndexesWithSection: 0 ]];
424
- }
425
420
} completion: NULL ];
421
+
422
+ NSIndexSet *changedIndexes = [collectionChanges changedIndexes ];
423
+ if ([changedIndexes count ]) {
424
+ [self .collectionView reloadItemsAtIndexPaths: [changedIndexes qb_indexPathsFromIndexesWithSection: 0 ]];
425
+ }
426
426
}
427
427
428
428
[self resetCachedAssets ];
429
+
430
+ // Update the selection to remove any assets that have been removed from the collection
431
+ NSMutableSet *removedAssets = [NSMutableSet new ];
432
+ for (PHAsset *asset in self.imagePickerController .selectedAssets ) {
433
+ if (![self .fetchResult containsObject: asset]) {
434
+ [removedAssets addObject: asset];
435
+ }
436
+ }
437
+ [self removeAssetsFromSelection: removedAssets];
438
+
439
+ // Update the footer to show the current photo/video counts
440
+ NSArray <UICollectionReusableView *> *footers =
441
+ [self .collectionView visibleSupplementaryViewsOfKind: UICollectionElementKindSectionFooter];
442
+ if (footers.count ) {
443
+ [self updateFooterView: footers[0 ]];
444
+ }
429
445
}
430
446
});
431
447
}
@@ -507,57 +523,60 @@ - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
507
523
UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind: UICollectionElementKindSectionFooter
508
524
withReuseIdentifier: @" FooterView"
509
525
forIndexPath: indexPath];
526
+ [self updateFooterView: footerView];
510
527
511
- // Number of assets
512
- UILabel *label = (UILabel *)[footerView viewWithTag: 1 ];
528
+ return footerView;
529
+ }
513
530
514
- NSBundle *bundle = self.imagePickerController .assetBundle ;
515
- NSUInteger numberOfPhotos = [self .fetchResult countOfAssetsWithMediaType: PHAssetMediaTypeImage];
516
- NSUInteger numberOfVideos = [self .fetchResult countOfAssetsWithMediaType: PHAssetMediaTypeVideo];
531
+ return nil ;
532
+ }
517
533
518
- switch (self.imagePickerController .mediaType ) {
519
- case QBImagePickerMediaTypeAny:
520
- {
521
- NSString *format;
522
- if (numberOfPhotos == 1 ) {
523
- if (numberOfVideos == 1 ) {
524
- format = NSLocalizedStringFromTableInBundle(@" assets.footer.photo-and-video" , @" QBImagePicker" , bundle, nil );
525
- } else {
526
- format = NSLocalizedStringFromTableInBundle(@" assets.footer.photo-and-videos" , @" QBImagePicker" , bundle, nil );
527
- }
528
- } else if (numberOfVideos == 1 ) {
529
- format = NSLocalizedStringFromTableInBundle(@" assets.footer.photos-and-video" , @" QBImagePicker" , bundle, nil );
534
+ - (void )updateFooterView : (UICollectionReusableView *)footerView {
535
+ // Number of assets
536
+ UILabel *label = (UILabel *)[footerView viewWithTag: 1 ];
537
+
538
+ NSBundle *bundle = self.imagePickerController .assetBundle ;
539
+ NSUInteger numberOfPhotos = [self .fetchResult countOfAssetsWithMediaType: PHAssetMediaTypeImage];
540
+ NSUInteger numberOfVideos = [self .fetchResult countOfAssetsWithMediaType: PHAssetMediaTypeVideo];
541
+
542
+ switch (self.imagePickerController .mediaType ) {
543
+ case QBImagePickerMediaTypeAny:
544
+ {
545
+ NSString *format;
546
+ if (numberOfPhotos == 1 ) {
547
+ if (numberOfVideos == 1 ) {
548
+ format = NSLocalizedStringFromTableInBundle(@" assets.footer.photo-and-video" , @" QBImagePicker" , bundle, nil );
530
549
} else {
531
- format = NSLocalizedStringFromTableInBundle(@" assets.footer.photos -and-videos" , @" QBImagePicker" , bundle, nil );
550
+ format = NSLocalizedStringFromTableInBundle(@" assets.footer.photo -and-videos" , @" QBImagePicker" , bundle, nil );
532
551
}
533
-
534
- label.text = [NSString stringWithFormat: format, numberOfPhotos, numberOfVideos];
552
+ } else if (numberOfVideos == 1 ) {
553
+ format = NSLocalizedStringFromTableInBundle(@" assets.footer.photos-and-video" , @" QBImagePicker" , bundle, nil );
554
+ } else {
555
+ format = NSLocalizedStringFromTableInBundle(@" assets.footer.photos-and-videos" , @" QBImagePicker" , bundle, nil );
535
556
}
536
- break ;
537
557
538
- case QBImagePickerMediaTypeImage:
539
- {
540
- NSString *key = (numberOfPhotos == 1 ) ? @" assets.footer.photo" : @" assets.footer.photos" ;
541
- NSString *format = NSLocalizedStringFromTableInBundle(key, @" QBImagePicker" , bundle, nil );
542
-
543
- label.text = [NSString stringWithFormat: format, numberOfPhotos];
544
- }
545
- break ;
558
+ label.text = [NSString stringWithFormat: format, numberOfPhotos, numberOfVideos];
559
+ }
560
+ break ;
546
561
547
- case QBImagePickerMediaTypeVideo :
548
- {
549
- NSString *key = (numberOfVideos == 1 ) ? @" assets.footer.video " : @" assets.footer.videos " ;
550
- NSString *format = NSLocalizedStringFromTableInBundle(key, @" QBImagePicker" , bundle, nil );
562
+ case QBImagePickerMediaTypeImage :
563
+ {
564
+ NSString *key = (numberOfPhotos == 1 ) ? @" assets.footer.photo " : @" assets.footer.photos " ;
565
+ NSString *format = NSLocalizedStringFromTableInBundle(key, @" QBImagePicker" , bundle, nil );
551
566
552
- label.text = [NSString stringWithFormat: format, numberOfVideos];
553
- }
554
- break ;
567
+ label.text = [NSString stringWithFormat: format, numberOfPhotos];
555
568
}
569
+ break ;
556
570
557
- return footerView;
558
- }
571
+ case QBImagePickerMediaTypeVideo:
572
+ {
573
+ NSString *key = (numberOfVideos == 1 ) ? @" assets.footer.video" : @" assets.footer.videos" ;
574
+ NSString *format = NSLocalizedStringFromTableInBundle(key, @" QBImagePicker" , bundle, nil );
559
575
560
- return nil ;
576
+ label.text = [NSString stringWithFormat: format, numberOfVideos];
577
+ }
578
+ break ;
579
+ }
561
580
}
562
581
563
582
@@ -653,6 +672,32 @@ - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndex
653
672
}
654
673
}
655
674
675
+ - (void )removeAssetsFromSelection : (NSSet *)assets
676
+ {
677
+ if (assets.count == 0 ) {
678
+ return ;
679
+ }
680
+
681
+ QBImagePickerController *imagePickerController = self.imagePickerController ;
682
+ NSMutableOrderedSet *selectedAssets = imagePickerController.selectedAssets ;
683
+
684
+ // Remove assets from set
685
+ [selectedAssets minusSet: assets];
686
+
687
+ self.lastSelectedItemIndexPath = nil ;
688
+
689
+ [self updateDoneButtonState ];
690
+
691
+ if (self.imagePickerController .showsNumberOfSelectedAssets ) {
692
+ [self updateSelectionInfo ];
693
+
694
+ if (selectedAssets.count == 0 ) {
695
+ // Hide toolbar
696
+ [self .navigationController setToolbarHidden: YES animated: YES ];
697
+ }
698
+ }
699
+ }
700
+
656
701
657
702
#pragma mark - UICollectionViewDelegateFlowLayout
658
703
0 commit comments