diff --git a/example/lib/main.dart b/example/lib/main.dart index 5b5f1e3d..f03ac44e 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -125,6 +125,7 @@ class _SimpleExamplePageState extends State<_SimpleExamplePage> { final List entities = await _path!.getAssetListPaged( page: 0, size: _sizePerPage, + type: RequestType.common, // Switch to .video or .image to filter album assets by format ); if (!mounted) { return; @@ -140,6 +141,7 @@ class _SimpleExamplePageState extends State<_SimpleExamplePage> { final List entities = await _path!.getAssetListPaged( page: _page + 1, size: _sizePerPage, + type: RequestType.common, // Switch to .video or .image to filter album assets by format ); if (!mounted) { return; diff --git a/ios/Classes/core/PMManager.m b/ios/Classes/core/PMManager.m index 9612f947..0bc4f4ce 100644 --- a/ios/Classes/core/PMManager.m +++ b/ios/Classes/core/PMManager.m @@ -259,37 +259,71 @@ - (void)injectAssetPathIntoArray:(NSMutableArray *)array - (NSArray *)getAssetListPaged:(NSString *)id type:(int)type page:(NSUInteger)page size:(NSUInteger)size filterOption:(NSObject *)filterOption { NSMutableArray *result = [NSMutableArray new]; - + PHFetchOptions *options = [PHFetchOptions new]; - + PHFetchResult *fetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[id] options:options]; if (fetchResult && fetchResult.count == 0) { return result; } - + PHAssetCollection *collection = fetchResult.firstObject; PHFetchOptions *assetOptions = [self getAssetOptions:type filterOption:filterOption]; + + // --- PATCH: enforce type filtering explicitly --- + // 'type' here is a bitmask based on PhotoManager's RequestType enum: + // 1 = image + // 2 = video + // 3 = common (image + video) <-- THIS IS THE "COMMON" CASE + // 4 = audio + if (type > 0) { + NSMutableArray *predicates = [NSMutableArray array]; + + // Bitmask logic for RequestType: + // 1 = image, 2 = video, 4 = audio + if (type & 1) { // image + [predicates addObject:[NSPredicate predicateWithFormat:@"mediaType == %d", PHAssetMediaTypeImage]]; + } + if (type & 2) { // video + [predicates addObject:[NSPredicate predicateWithFormat:@"mediaType == %d", PHAssetMediaTypeVideo]]; + } + if (type & 4) { // audio + [predicates addObject:[NSPredicate predicateWithFormat:@"mediaType == %d", PHAssetMediaTypeAudio]]; + } + + // Combine predicates with OR: fetch assets matching any of the selected types + if (predicates.count > 0) { + NSPredicate *typePredicate = [NSCompoundPredicate orPredicateWithSubpredicates:predicates]; + // If there is already a predicate (from filterOption), combine with AND + if (assetOptions.predicate) { + assetOptions.predicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[assetOptions.predicate, typePredicate]]; + } else { + assetOptions.predicate = typePredicate; + } + } + } + // --- end patch --- + NSArray *sortDescriptors = assetOptions.sortDescriptors; PHFetchResult *assetArray = [PHAsset fetchAssetsInAssetCollection:collection options:assetOptions]; - if (assetArray.count == 0) { return result; } - + NSUInteger startIndex = page * size; NSUInteger endIndex = startIndex + size - 1; - + NSUInteger count = assetArray.count; if (endIndex >= count) { endIndex = count - 1; } - + BOOL imageNeedTitle = filterOption.needTitle; BOOL videoNeedTitle = filterOption.needTitle; - + for (NSUInteger i = startIndex; i <= endIndex; i++) { NSUInteger index = i; if (sortDescriptors == nil || sortDescriptors.count == 0) { @@ -306,7 +340,7 @@ - (void)injectAssetPathIntoArray:(NSMutableArray *)array [result addObject:entity]; [cacheContainer putAssetEntity:entity]; } - + return result; } diff --git a/lib/src/types/entity.dart b/lib/src/types/entity.dart index dceed70a..958a5255 100644 --- a/lib/src/types/entity.dart +++ b/lib/src/types/entity.dart @@ -195,6 +195,7 @@ class AssetPathEntity { Future> getAssetListPaged({ required int page, required int size, + RequestType? type, // allow overriding the album type per call }) { assert(albumType == 1, 'Only album can request for assets.'); assert(size > 0, 'Page size must be greater than 0.'); @@ -202,7 +203,7 @@ class AssetPathEntity { id, page: page, size: size, - type: type, + type: type ?? RequestType.common, // use album type by default optionGroup: filterOption, ); } diff --git a/pubspec.yaml b/pubspec.yaml index eca9ec87..111f5226 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: photo_manager description: A Flutter plugin that provides album assets abstraction management APIs on Android, iOS, macOS, and OpenHarmony. repository: https://github.com/fluttercandies/flutter_photo_manager -version: 3.7.1 +version: 3.7.2 environment: sdk: ">=2.13.0 <4.0.0"