Skip to content

Commit

Permalink
#11: Fixed getting file names for assets on iOS
Browse files Browse the repository at this point in the history
There is a bug in assetResourcesForAsset - it doesn't work properly for all the images.
Moreover, it obtains resource for very long time - too long for just a file name.
Implementation was replaced with getting file name from asset properties directly - quite hacky however.
  • Loading branch information
domax committed Mar 4, 2017
1 parent b917424 commit f150259
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cordova-plugin-photos",
"version": "1.0.7",
"version": "1.0.8",
"description": "This Cordova/Phonegap plugin provides access to photo library on device.",
"cordova": {
"id": "cordova-plugin-photos",
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="cordova-plugin-photos"
version="1.0.7">
version="1.0.8">

<name>Photos</name>
<keywords>cordova, camera, file, exif, geo, location, geolocation, tag</keywords>
Expand Down
56 changes: 33 additions & 23 deletions src/ios/CDVPhotos.m
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ - (void) photos:(CDVInvokedUrlCommand*)command {
}

int __block fetched = 0;
NSMutableArray<NSDictionary*>* result = [NSMutableArray array];
NSMutableArray<PHAsset*>* __block skippedAssets = [NSMutableArray array];
NSMutableArray<NSDictionary*>* __block result = [NSMutableArray array];
[fetchResultAssetCollections enumerateObjectsUsingBlock:
^(PHAssetCollection* _Nonnull assetCollection, NSUInteger idx, BOOL* _Nonnull stop) {
if ([weakSelf isNull:weakSelf.photosCommand]) {
Expand All @@ -185,19 +186,16 @@ - (void) photos:(CDVInvokedUrlCommand*)command {
*stop = YES;
return;
}
PHAssetResource* resource = [weakSelf resourceForAsset:asset];
if (resource != nil) {
NSString* filename = [weakSelf getFilenameForAsset:asset];
if (![weakSelf isNull:filename]) {
NSTextCheckingResult* match
= [weakSelf.extRegex
firstMatchInString:resource.originalFilename
firstMatchInString:filename
options:0
range:NSMakeRange(0, resource.originalFilename.length)];
range:NSMakeRange(0, filename.length)];
if (match != nil) {
NSString* name = [resource.originalFilename
substringWithRange:[match rangeAtIndex:1]];
NSString* ext = [[resource.originalFilename
substringWithRange:[match rangeAtIndex:2]]
uppercaseString];
NSString* name = [filename substringWithRange:[match rangeAtIndex:1]];
NSString* ext = [[filename substringWithRange:[match rangeAtIndex:2]] uppercaseString];
NSString* type = weakSelf.extType[ext];
if (![weakSelf isNull:type]) {
if (offset <= fetched) {
Expand All @@ -224,11 +222,17 @@ - (void) photos:(CDVInvokedUrlCommand*)command {
}
}
++fetched;
}
}
}
} else [skippedAssets addObject:asset];
} else [skippedAssets addObject:asset];
} else [skippedAssets addObject:asset];
}];
}];
[skippedAssets enumerateObjectsUsingBlock:^(PHAsset* _Nonnull asset, NSUInteger idx, BOOL* _Nonnull stop) {
NSLog(@"skipped asset %lu: id=%@; name=%@, type=%ld-%ld; size=%lux%lu;",
idx, asset.localIdentifier, [weakSelf getFilenameForAsset:asset],
(long)asset.mediaType, (long)asset.mediaSubtypes,
(unsigned long)asset.pixelWidth, asset.pixelHeight);
}];
weakSelf.photosCommand = nil;
[weakSelf success:command withArray:result];
}];
Expand Down Expand Up @@ -386,16 +390,22 @@ - (PHAsset*) assetByCommand:(CDVInvokedUrlCommand*)command {
return asset;
}

- (PHAssetResource*) resourceForAsset:(PHAsset*)asset {
PHAssetResource* __block result = nil;
[[PHAssetResource assetResourcesForAsset:asset] enumerateObjectsUsingBlock:
^(PHAssetResource* _Nonnull resource, NSUInteger idx, BOOL* _Nonnull stop) {
if (resource.type == PHAssetResourceTypePhoto) {
result = resource;
*stop = YES;
}
}];
return result;
- (NSString*) getFilenameForAsset:(PHAsset*)asset {
// Works fine, but asynchronous ((.
// [asset
// requestContentEditingInputWithOptions:nil
// completionHandler:^(PHContentEditingInput* _Nullable contentEditingInput, NSDictionary* _Nonnull info) {
// NSString* filename = [[contentEditingInput.fullSizeImageURL.absoluteString componentsSeparatedByString:@"/"] lastObject];
// }];

// Most optimal and fast, but it's dirty hack
return [asset valueForKey:@"filename"];

// assetResourcesForAsset doesn't work properly for all images.
// Moreover, it obtains resource for very long time - too long for just a file name.
// NSArray<PHAssetResource*>* resources = [PHAssetResource assetResourcesForAsset:asset];
// if ([self isNull:resources] || resources.count == 0) return nil;
// return resources[0].originalFilename;
}

- (PHFetchResult<PHAssetCollection*>*) fetchCollections:(NSDictionary*)options {
Expand Down

0 comments on commit f150259

Please sign in to comment.