Skip to content

Commit 4c76828

Browse files
limitation for video size
1 parent 7e81273 commit 4c76828

File tree

4 files changed

+89
-10
lines changed

4 files changed

+89
-10
lines changed

src/ios/GMImagePicker/GMGridViewController.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
@interface GMGridViewController : UICollectionViewController
1818

1919
@property (strong) PHFetchResult *assetsFetchResults;
20+
@property (nonatomic, strong) NSArray *filteredAssets;
2021
@property (nonatomic, weak) NSMutableDictionary * dic_asset_fetches;
2122

2223
-(id)initWithPicker:(GMImagePickerController *)picker;

src/ios/GMImagePicker/GMGridViewController.m

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

src/ios/SOSPicker.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@
2525
@property (nonatomic, assign) NSInteger height;
2626
@property (nonatomic, assign) NSInteger quality;
2727
@property (nonatomic, assign) NSInteger outputType;
28+
@property (nonatomic, assign) NSInteger maxMB;
2829

2930
@end

src/ios/SOSPicker.m

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
//
88

99
#import "SOSPicker.h"
10+
#import <Photos/Photos.h>
11+
#import <AVFoundation/AVFoundation.h>
1012
#import "GMImagePickerController.h"
1113
#import "GMFetchItem.h"
1214

@@ -65,6 +67,7 @@ - (void) getPictures:(CDVInvokedUrlCommand *)command {
6567
self.width = [[options objectForKey:@"width"] integerValue];
6668
self.height = [[options objectForKey:@"height"] integerValue];
6769
self.quality = [[options objectForKey:@"quality"] integerValue];
70+
self.maxMB = [[options objectForKey:@"maxFileSize"] integerValue];
6871

6972
self.callbackId = command.callbackId;
7073
[self launchGMImagePicker:allow_video title:title message:message disable_popover:disable_popover maximumImagesCount:maximumImagesCount];
@@ -435,4 +438,42 @@ - (void) closeImagePicker:(CDVInvokedUrlCommand *)command {
435438
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
436439
}
437440

441+
#pragma mark - GMImagePickerControllerDelegate - Asset Filtering
442+
443+
- (BOOL)assetsPickerController:(GMImagePickerController *)picker shouldShowAsset:(PHAsset *)asset {
444+
if (asset.mediaType != PHAssetMediaTypeVideo) {
445+
return YES;
446+
}
447+
448+
NSArray *resources = [PHAssetResource assetResourcesForAsset:asset];
449+
long long fileSize = 0;
450+
451+
for (PHAssetResource *resource in resources) {
452+
if (resource.type == PHAssetResourceTypeVideo ||
453+
resource.type == PHAssetResourceTypeFullSizeVideo ||
454+
resource.type == PHAssetResourceTypePairedVideo) {
455+
456+
@try {
457+
id fileSizeValue = [resource valueForKey:@"fileSize"];
458+
if ([fileSizeValue isKindOfClass:[NSNumber class]]) {
459+
fileSize = [fileSizeValue longLongValue];
460+
break;
461+
}
462+
} @catch (NSException *exception) {
463+
// Continue to next resource if fileSize is not available
464+
}
465+
}
466+
}
467+
468+
if (fileSize > 0) {
469+
CGFloat mb = fileSize / (1024.0 * 1024.0);
470+
471+
if (mb > self.maxMB) {
472+
return NO;
473+
}
474+
return YES;
475+
}
476+
return YES;
477+
}
478+
438479
@end

0 commit comments

Comments
 (0)