Skip to content

Commit

Permalink
Modify the titleAsync implementation on iOS. (#740)
Browse files Browse the repository at this point in the history
On iOS, titleAsync is modified to PHAssetResource.originalFileName.
  • Loading branch information
CaiJingLong authored Mar 25, 2022
1 parent a8c193e commit 98e8f68
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## 2.0.5

Improvements:
- Improve `AssetEntity.titleAsync`'s implementation on iOS. (#740)

## 2.0.4

Fixes:
Expand Down
3 changes: 2 additions & 1 deletion ios/Classes/PMPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,8 @@ - (void)onAuth:(FlutterMethodCall *)call result:(FlutterResult)result {
[handler reply:@(exists)];
} else if ([call.method isEqualToString:@"getTitleAsync"]) {
NSString *assetId = call.arguments[@"id"];
NSString *title = [manager getTitleAsyncWithAssetId:assetId];
int subtype = [call.arguments[@"subtype"] intValue];
NSString *title = [manager getTitleAsyncWithAssetId:assetId subtype:subtype];
[handler reply:title];
} else if ([call.method isEqualToString:@"getMimeTypeAsync"]) {
NSString *assetId = call.arguments[@"id"];
Expand Down
3 changes: 3 additions & 0 deletions ios/Classes/core/PHAsset+PM_COMMON.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ NS_ASSUME_NONNULL_BEGIN
- (int)unwrappedSubtype;

- (NSString*)title;

- (NSString *)originalFilenameWithSubtype:(int) subtype;

/**
Get the MIME type for this asset from UTI (`PHAssetResource.uniformTypeIdentifier`), such as `image/jpeg`, `image/heic`, `video/quicktime`, etc.
Expand Down
19 changes: 19 additions & 0 deletions ios/Classes/core/PHAsset+PM_COMMON.m
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,25 @@ - (NSString *)title {
return @"";
}
}

- (NSString *)originalFilenameWithSubtype:(int)subtype {
if (@available(iOS 9.1, *)) {
if ([self isLivePhoto] && subtype == PHAssetMediaSubtypePhotoLive) {
return [self getLivePhotosResource].originalFilename;
}
}
if (@available(macOS 10.11, *)) {
if ([self isLivePhoto] && subtype == PHAssetMediaSubtypePhotoLive) {
return [self getLivePhotosResource].originalFilename;
}
}
PHAssetResource *resource = [self getAdjustResource];
if (resource) {
return resource.originalFilename;
}
return @"";
}

// UTI: https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/understanding_utis/understand_utis_intro/understand_utis_intro.html#//apple_ref/doc/uid/TP40001319
- (NSString *)mimeType {
PHAssetResource *resource = [[PHAssetResource assetResourcesForAsset:self] firstObject];
Expand Down
2 changes: 1 addition & 1 deletion ios/Classes/core/PMManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ typedef void (^AssetResult)(PMAssetEntity *);

- (BOOL)entityIsLocallyAvailable:(NSString *)assetId resource:(PHAssetResource *)resource isOrigin:(BOOL)isOrigin;

- (NSString*)getTitleAsyncWithAssetId: (NSString *) assetId;
- (NSString*)getTitleAsyncWithAssetId:(NSString *)assetId subtype:(int)subtype;

- (NSString*)getMimeTypeAsyncWithAssetId: (NSString *) assetId;

Expand Down
4 changes: 2 additions & 2 deletions ios/Classes/core/PMManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -1079,10 +1079,10 @@ - (void)saveVideo:(NSString *)path
}];
}

- (NSString *)getTitleAsyncWithAssetId:(NSString *)assetId {
- (NSString *)getTitleAsyncWithAssetId:(NSString *)assetId subtype:(int)subtype {
PHAsset *asset = [PHAsset fetchAssetsWithLocalIdentifiers:@[assetId] options:nil].firstObject;
if (asset) {
return [asset title];
return [asset originalFilenameWithSubtype:subtype];
}
return @"";
}
Expand Down
10 changes: 8 additions & 2 deletions lib/src/internal/plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -332,15 +332,21 @@ class PhotoManagerPlugin with BasePlugin, IosPlugin, AndroidPlugin {
return LatLng(latitude: entity.latitude, longitude: entity.longitude);
}

Future<String> getTitleAsync(AssetEntity entity) async {
Future<String> getTitleAsync(
AssetEntity entity, {
int subtype = 0,
}) async {
assert(Platform.isAndroid || Platform.isIOS || Platform.isMacOS);
if (Platform.isAndroid) {
return entity.title!;
}
if (Platform.isIOS || Platform.isMacOS) {
return await _channel.invokeMethod<String>(
PMConstants.mGetTitleAsync,
<String, dynamic>{'id': entity.id},
<String, dynamic>{
'id': entity.id,
'subtype': subtype,
},
) as String;
}
return '';
Expand Down
8 changes: 7 additions & 1 deletion lib/src/types/entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,16 @@ class AssetEntity {
/// or use the async getter [titleAsync].
final String? title;

/// {@template photo_manager.AssetEntity.titleAsync}
/// * Android: `MediaStore.MediaColumns.DISPLAY_NAME`.
/// * iOS/macOS: `PHAssetResource.filename`.
/// * iOS/macOS: `PHAssetResource.originalFilename`.
/// {@endtemplate}
Future<String> get titleAsync => plugin.getTitleAsync(this);

/// {@macro photo_manager.AssetEntity.titleAsync}
Future<String> get titleAsyncWithSubtype =>
plugin.getTitleAsync(this, subtype: subtype);

/// {@macro photo_manager.AssetType}
AssetType get type => AssetType.values[typeInt];

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: photo_manager
description: A Flutter plugin that provides assets abstraction management APIs on Android, iOS, and macOS.
version: 2.0.4
version: 2.0.5
homepage: https://github.com/fluttercandies/flutter_photo_manager

environment:
Expand Down

0 comments on commit 98e8f68

Please sign in to comment.