@@ -82,6 +82,12 @@ @implementation GMGridViewController
8282
8383@synthesize dic_asset_fetches;
8484
85+ - (void )setAssetsFetchResults : (PHFetchResult *)assetsFetchResults
86+ {
87+ _assetsFetchResults = assetsFetchResults;
88+ [self filterAssets ];
89+ }
90+
8591-(id )initWithPicker : (GMImagePickerController *)picker
8692{
8793 // Custom init. The picker contains custom information to create the FlowLayout
@@ -147,6 +153,7 @@ - (void)viewDidLoad
147153 }
148154
149155 self.imageManager = [[PHCachingImageManager alloc ] init ];
156+ [self filterAssets ];
150157 [self resetCachedAssets ];
151158 [[PHPhotoLibrary sharedPhotoLibrary ] registerChangeObserver: self ];
152159
@@ -222,6 +229,34 @@ - (void)setupViews
222229 self.collectionView .backgroundColor = [UIColor whiteColor ];
223230}
224231
232+ - (void )filterAssets
233+ {
234+ if (!self.assetsFetchResults ) {
235+ self.filteredAssets = @[];
236+ return ;
237+ }
238+
239+ NSMutableArray *filtered = [NSMutableArray array ];
240+
241+ // Check if delegate implements shouldShowAsset method
242+ BOOL implementsFilter = [self .picker.delegate respondsToSelector: @selector (assetsPickerController:shouldShowAsset: )];
243+
244+ for (NSInteger i = 0 ; i < self.assetsFetchResults .count ; i++) {
245+ PHAsset *asset = self.assetsFetchResults [i];
246+
247+ if (implementsFilter) {
248+ if ([self .picker.delegate assetsPickerController: self .picker shouldShowAsset: asset]) {
249+ [filtered addObject: asset];
250+ }
251+ } else {
252+ // If delegate doesn't implement the method, show all assets
253+ [filtered addObject: asset];
254+ }
255+ }
256+
257+ self.filteredAssets = [filtered copy ];
258+ }
259+
225260- (void )setupButtons
226261{
227262 self.navigationItem .rightBarButtonItem =
@@ -310,7 +345,7 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell
310345 NSInteger currentTag = cell.tag + 1 ;
311346 cell.tag = currentTag;
312347
313- PHAsset *asset = self.assetsFetchResults [indexPath.item];
348+ PHAsset *asset = self.filteredAssets [indexPath.item];
314349 [cell bind: asset];
315350
316351 // GMFetchItem * fetch_item = [dic_asset_fetches objectForKey:[NSNumber numberWithLong:indexPath.item] ];
@@ -438,7 +473,7 @@ - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtInde
438473 return NO ;
439474 }
440475
441- PHAsset *asset = self.assetsFetchResults [indexPath.item];
476+ PHAsset *asset = self.filteredAssets [indexPath.item];
442477 // GMFetchItem * fetch_item = [dic_asset_fetches objectForKey:[ NSNumber numberWithLong:indexPath.item ]];
443478 GMFetchItem * fetch_item = [dic_asset_fetches objectForKey: asset];
444479
@@ -580,7 +615,7 @@ - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtInde
580615
581616- (void )collectionView : (UICollectionView *)collectionView didSelectItemAtIndexPath : (NSIndexPath *)indexPath
582617{
583- PHAsset *asset = self.assetsFetchResults [indexPath.item];
618+ PHAsset *asset = self.filteredAssets [indexPath.item];
584619 // GMFetchItem * fetch_item = [dic_asset_fetches objectForKey:[ NSNumber numberWithLong:indexPath.item ]];
585620 GMFetchItem * fetch_item = [dic_asset_fetches objectForKey: asset];
586621
@@ -593,7 +628,7 @@ - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPa
593628
594629- (BOOL )collectionView : (UICollectionView *)collectionView shouldDeselectItemAtIndexPath : (NSIndexPath *)indexPath
595630{
596- PHAsset *asset = self.assetsFetchResults [indexPath.item];
631+ PHAsset *asset = self.filteredAssets [indexPath.item];
597632
598633 if ([self .picker.delegate respondsToSelector: @selector (assetsPickerController:shouldDeselectAsset: )])
599634 return [self .picker.delegate assetsPickerController: self .picker shouldDeselectAsset: asset];
@@ -603,7 +638,7 @@ - (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIn
603638
604639- (void )collectionView : (UICollectionView *)collectionView didDeselectItemAtIndexPath : (NSIndexPath *)indexPath
605640{
606- PHAsset *asset = self.assetsFetchResults [indexPath.item];
641+ PHAsset *asset = self.filteredAssets [indexPath.item];
607642 // GMFetchItem * fetch_item = [dic_asset_fetches objectForKey:[ NSNumber numberWithLong:indexPath.item ]];
608643 GMFetchItem * fetch_item = [dic_asset_fetches objectForKey: asset];
609644
@@ -616,7 +651,7 @@ - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndex
616651
617652- (BOOL )collectionView : (UICollectionView *)collectionView shouldHighlightItemAtIndexPath : (NSIndexPath *)indexPath
618653{
619- PHAsset *asset = self.assetsFetchResults [indexPath.item];
654+ PHAsset *asset = self.filteredAssets [indexPath.item];
620655
621656 if ([self .picker.delegate respondsToSelector: @selector (assetsPickerController:shouldHighlightAsset: )])
622657 return [self .picker.delegate assetsPickerController: self .picker shouldHighlightAsset: asset];
@@ -626,15 +661,15 @@ - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtI
626661
627662- (void )collectionView : (UICollectionView *)collectionView didHighlightItemAtIndexPath : (NSIndexPath *)indexPath
628663{
629- PHAsset *asset = self.assetsFetchResults [indexPath.item];
664+ PHAsset *asset = self.filteredAssets [indexPath.item];
630665
631666 if ([self .picker.delegate respondsToSelector: @selector (assetsPickerController:didHighlightAsset: )])
632667 [self .picker.delegate assetsPickerController: self .picker didHighlightAsset: asset];
633668}
634669
635670- (void )collectionView : (UICollectionView *)collectionView didUnhighlightItemAtIndexPath : (NSIndexPath *)indexPath
636671{
637- PHAsset *asset = self.assetsFetchResults [indexPath.item];
672+ PHAsset *asset = self.filteredAssets [indexPath.item];
638673
639674 if ([self .picker.delegate respondsToSelector: @selector (assetsPickerController:didUnhighlightAsset: )])
640675 [self .picker.delegate assetsPickerController: self .picker didUnhighlightAsset: asset];
@@ -646,7 +681,7 @@ - (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIn
646681
647682- (NSInteger )collectionView : (UICollectionView *)collectionView numberOfItemsInSection : (NSInteger )section
648683{
649- NSInteger count = self.assetsFetchResults .count ;
684+ NSInteger count = self.filteredAssets .count ;
650685 return count;
651686}
652687
@@ -664,6 +699,7 @@ - (void)photoLibraryDidChange:(PHChange *)changeInstance
664699
665700 // get the new fetch result
666701 self.assetsFetchResults = [collectionChanges fetchResultAfterChanges ];
702+ [self filterAssets ];
667703
668704 // NSLog( @"reset all" );
669705
@@ -788,7 +824,7 @@ - (NSArray *)assetsAtIndexPaths:(NSArray *)indexPaths
788824
789825 NSMutableArray *assets = [NSMutableArray arrayWithCapacity: indexPaths.count];
790826 for (NSIndexPath *indexPath in indexPaths) {
791- PHAsset *asset = self.assetsFetchResults [indexPath.item];
827+ PHAsset *asset = self.filteredAssets [indexPath.item];
792828 [assets addObject: asset];
793829 }
794830 return assets;
0 commit comments