快速获取PHAsset信息?
Get PHAsset information fast?
我有一个方法可以检索有关 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];
}
}];
}
最近为 Swift 3 添加了一个名为 AssetManager 的小型库 GitHub。
它简化了对资产的访问并允许非常快速的数据提取。
AssetManager 的 link 可以在这里找到:https://github.com/aidv/AssetManager
我有一个方法可以检索有关 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];
}
}];
}
最近为 Swift 3 添加了一个名为 AssetManager 的小型库 GitHub。
它简化了对资产的访问并允许非常快速的数据提取。
AssetManager 的 link 可以在这里找到:https://github.com/aidv/AssetManager