Skip to content

Commit f150259

Browse files
committed
#11: Fixed getting file names for assets on iOS
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.
1 parent b917424 commit f150259

File tree

3 files changed

+35
-25
lines changed

3 files changed

+35
-25
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cordova-plugin-photos",
3-
"version": "1.0.7",
3+
"version": "1.0.8",
44
"description": "This Cordova/Phonegap plugin provides access to photo library on device.",
55
"cordova": {
66
"id": "cordova-plugin-photos",

plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
33
xmlns:android="http://schemas.android.com/apk/res/android"
44
id="cordova-plugin-photos"
5-
version="1.0.7">
5+
version="1.0.8">
66

77
<name>Photos</name>
88
<keywords>cordova, camera, file, exif, geo, location, geolocation, tag</keywords>

src/ios/CDVPhotos.m

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ - (void) photos:(CDVInvokedUrlCommand*)command {
162162
}
163163

164164
int __block fetched = 0;
165-
NSMutableArray<NSDictionary*>* result = [NSMutableArray array];
165+
NSMutableArray<PHAsset*>* __block skippedAssets = [NSMutableArray array];
166+
NSMutableArray<NSDictionary*>* __block result = [NSMutableArray array];
166167
[fetchResultAssetCollections enumerateObjectsUsingBlock:
167168
^(PHAssetCollection* _Nonnull assetCollection, NSUInteger idx, BOOL* _Nonnull stop) {
168169
if ([weakSelf isNull:weakSelf.photosCommand]) {
@@ -185,19 +186,16 @@ - (void) photos:(CDVInvokedUrlCommand*)command {
185186
*stop = YES;
186187
return;
187188
}
188-
PHAssetResource* resource = [weakSelf resourceForAsset:asset];
189-
if (resource != nil) {
189+
NSString* filename = [weakSelf getFilenameForAsset:asset];
190+
if (![weakSelf isNull:filename]) {
190191
NSTextCheckingResult* match
191192
= [weakSelf.extRegex
192-
firstMatchInString:resource.originalFilename
193+
firstMatchInString:filename
193194
options:0
194-
range:NSMakeRange(0, resource.originalFilename.length)];
195+
range:NSMakeRange(0, filename.length)];
195196
if (match != nil) {
196-
NSString* name = [resource.originalFilename
197-
substringWithRange:[match rangeAtIndex:1]];
198-
NSString* ext = [[resource.originalFilename
199-
substringWithRange:[match rangeAtIndex:2]]
200-
uppercaseString];
197+
NSString* name = [filename substringWithRange:[match rangeAtIndex:1]];
198+
NSString* ext = [[filename substringWithRange:[match rangeAtIndex:2]] uppercaseString];
201199
NSString* type = weakSelf.extType[ext];
202200
if (![weakSelf isNull:type]) {
203201
if (offset <= fetched) {
@@ -224,11 +222,17 @@ - (void) photos:(CDVInvokedUrlCommand*)command {
224222
}
225223
}
226224
++fetched;
227-
}
228-
}
229-
}
225+
} else [skippedAssets addObject:asset];
226+
} else [skippedAssets addObject:asset];
227+
} else [skippedAssets addObject:asset];
230228
}];
231229
}];
230+
[skippedAssets enumerateObjectsUsingBlock:^(PHAsset* _Nonnull asset, NSUInteger idx, BOOL* _Nonnull stop) {
231+
NSLog(@"skipped asset %lu: id=%@; name=%@, type=%ld-%ld; size=%lux%lu;",
232+
idx, asset.localIdentifier, [weakSelf getFilenameForAsset:asset],
233+
(long)asset.mediaType, (long)asset.mediaSubtypes,
234+
(unsigned long)asset.pixelWidth, asset.pixelHeight);
235+
}];
232236
weakSelf.photosCommand = nil;
233237
[weakSelf success:command withArray:result];
234238
}];
@@ -386,16 +390,22 @@ - (PHAsset*) assetByCommand:(CDVInvokedUrlCommand*)command {
386390
return asset;
387391
}
388392

389-
- (PHAssetResource*) resourceForAsset:(PHAsset*)asset {
390-
PHAssetResource* __block result = nil;
391-
[[PHAssetResource assetResourcesForAsset:asset] enumerateObjectsUsingBlock:
392-
^(PHAssetResource* _Nonnull resource, NSUInteger idx, BOOL* _Nonnull stop) {
393-
if (resource.type == PHAssetResourceTypePhoto) {
394-
result = resource;
395-
*stop = YES;
396-
}
397-
}];
398-
return result;
393+
- (NSString*) getFilenameForAsset:(PHAsset*)asset {
394+
// Works fine, but asynchronous ((.
395+
// [asset
396+
// requestContentEditingInputWithOptions:nil
397+
// completionHandler:^(PHContentEditingInput* _Nullable contentEditingInput, NSDictionary* _Nonnull info) {
398+
// NSString* filename = [[contentEditingInput.fullSizeImageURL.absoluteString componentsSeparatedByString:@"/"] lastObject];
399+
// }];
400+
401+
// Most optimal and fast, but it's dirty hack
402+
return [asset valueForKey:@"filename"];
403+
404+
// assetResourcesForAsset doesn't work properly for all images.
405+
// Moreover, it obtains resource for very long time - too long for just a file name.
406+
// NSArray<PHAssetResource*>* resources = [PHAssetResource assetResourcesForAsset:asset];
407+
// if ([self isNull:resources] || resources.count == 0) return nil;
408+
// return resources[0].originalFilename;
399409
}
400410

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

0 commit comments

Comments
 (0)