Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class _SimpleExamplePageState extends State<_SimpleExamplePage> {
final List<AssetEntity> 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;
Expand All @@ -140,6 +141,7 @@ class _SimpleExamplePageState extends State<_SimpleExamplePage> {
final List<AssetEntity> 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;
Expand Down
52 changes: 43 additions & 9 deletions ios/Classes/core/PMManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -259,37 +259,71 @@ - (void)injectAssetPathIntoArray:(NSMutableArray<PMAssetPathEntity *> *)array

- (NSArray<PMAssetEntity *> *)getAssetListPaged:(NSString *)id type:(int)type page:(NSUInteger)page size:(NSUInteger)size filterOption:(NSObject<PMBaseFilter> *)filterOption {
NSMutableArray<PMAssetEntity *> *result = [NSMutableArray new];

PHFetchOptions *options = [PHFetchOptions new];

PHFetchResult<PHAssetCollection *> *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<NSSortDescriptor *> *sortDescriptors = assetOptions.sortDescriptors;
PHFetchResult<PHAsset *> *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) {
Expand All @@ -306,7 +340,7 @@ - (void)injectAssetPathIntoArray:(NSMutableArray<PMAssetPathEntity *> *)array
[result addObject:entity];
[cacheContainer putAssetEntity:entity];
}

return result;
}

Expand Down
3 changes: 2 additions & 1 deletion lib/src/types/entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,15 @@ class AssetPathEntity {
Future<List<AssetEntity>> 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.');
return plugin.getAssetListPaged(
id,
page: page,
size: size,
type: type,
type: type ?? RequestType.common, // use album type by default
optionGroup: filterOption,
);
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down