獲取視頻PHAsset對(duì)象
PHFetchOptions *option = [[PHFetchOptions alloc] init];
option.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld",PHAssetMediaTypeVideo];
option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
//fetchAssetCollectionsWithType
PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
for (PHAssetCollection *collection in smartAlbums) {
// 有可能是PHCollectionList類的的對(duì)象转捕,過濾掉
if (![collection isKindOfClass:[PHAssetCollection class]]) continue;
// 過濾空相冊
if (collection.estimatedAssetCount <= 0) continue;
if ([self isCameraRollAlbum:collection]) {
PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:collection options:option];
[fetchResult enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
PHAsset *phAsset = (PHAsset *)obj;
//可通過此PHAsset用下邊方法分別獲取時(shí)常、地址及縮略圖
}];
}
}
//獲取視頻本地地址及時(shí)長
- (void)getVideoPathFromPHAsset:(PHAsset *)asset Complete:(void (^)(NSString *filePatch,NSString *dTime))result {
PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
options.version = PHImageRequestOptionsVersionCurrent;
options.deliveryMode = PHVideoRequestOptionsDeliveryModeAutomatic;
[[PHImageManager defaultManager] requestAVAssetForVideo:asset options:options resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
NSString * sandboxExtensionTokenKey = info[@"PHImageFileSandboxExtensionTokenKey"];
NSArray * arr = [sandboxExtensionTokenKey componentsSeparatedByString:@";"];
NSString * filePath = arr[arr.count - 1];
CMTime time = [asset duration];
int seconds = ceil(time.value/time.timescale);
//format of minute
NSString *str_minute = [NSString stringWithFormat:@"%d",seconds/60];
//format of second
NSString *str_second = [NSString stringWithFormat:@"%.2d",seconds%60];
//format of time
NSString *format_time = [NSString stringWithFormat:@"%@:%@",str_minute,str_second];
result(filePath,format_time);
}];
}
//獲取視頻縮略圖
- (void)getVideoImageFromPHAsset:(PHAsset *)asset Complete:(void (^)(UIImage *image))resultBack{
PHImageRequestOptions *option = [[PHImageRequestOptions alloc] init];
option.resizeMode = PHImageRequestOptionsResizeModeFast;
[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:CGSizeMake(200, 200) contentMode:PHImageContentModeDefault options:option resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
UIImage *iamge = result;
resultBack(iamge);
}];
}