快速获取PHAsset信息?

时间:2015-09-03 13:51:53

标签: ios xcode optimization metadata fileinfo

我有一个方法可以检索有关PHAsset的信息,但是它有效,但是当涉及到读取数百个资产的信息时,它可能会很慢。

阅读有关资产(如文件大小和元数据)的信息的最佳方法是什么?

我目前的代码是:

- (void) getAssetInfo: (NSUInteger*) assetIndex {
    NSNumber *_assetIndex = [NSNumber numberWithUnsignedInteger: assetIndex];

    PHFetchOptions *fetchOptions;
    fetchOptions.sortDescriptors = @[ [NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES], ];
    PHFetchResult *fetchResult = [PHAsset fetchAssetsWithOptions:fetchOptions];

    PHAsset *asset = [fetchResult objectAtIndex: assetIndex];

    PHImageManager *imageManager = [PHImageManager defaultManager];
    PHImageRequestOptions *options = [[PHImageRequestOptions alloc]init];
    options.synchronous = YES;
    options.version = PHImageRequestOptionsVersionCurrent;


    [asset requestContentEditingInputWithOptions:options
                               completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {

                                   NSString *fileInfo;

                                   NSString *assetURL = nil;

                                   if (contentEditingInput.avAsset != nil){

                                       AVURLAsset *avurlasset_ = (AVURLAsset*) contentEditingInput.avAsset;
                                       assetURL = [avurlasset_.URL absoluteString];

                                   } else {
                                       assetURL = [contentEditingInput.fullSizeImageURL absoluteString];
                                   }

                                   if (assetURL != nil){
                                       uint64_t fileSize;

                                       NSString *filePathFull = [assetURL substringWithRange: NSMakeRange(7, [assetURL length] - 7)];

                                       NSFileManager * filemanager = [[NSFileManager alloc]init];
                                       if([filemanager fileExistsAtPath:filePathFull]){
                                           fileSize = [[filemanager attributesOfItemAtPath:filePathFull error:nil] fileSize];
                                       }


                                       NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                                       [dateFormatter setDateFormat:@"yyyy,MM,dd,HH,mm,ss"];

                                       NSArray *assembled_FI = [NSArray arrayWithObjects:
                                                                (NSNumber*) _assetIndex,
                                                                assetURL,
                                                                assetURL.lastPathComponent,
                                                                (NSNumber*)[NSNumber numberWithUnsignedLongLong: fileSize],
                                                                (NSString*) [dateFormatter stringFromDate:asset.creationDate],
                                                                nil];
                                }
                               }];
}

1 个答案:

答案 0 :(得分:0)

最近在GitHub中添加了一个名为AssetManager的小型库。

它简化了对资产的访问,并允许非常快速的数据提取。

可以在此处找到AssetManager的链接:https://github.com/aidv/AssetManager