iOS--通过assetURL获取到视频

首先导入头文件

1
#import <Photos/Photos.h>

使用当前的URL过滤到我们想要的数据

1
2
3
4
5
6
7
8
9
10
11
12
13
PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[assetUrl] options:nil];
PHAsset * asset = result.firstObject;
/// 包含该视频的基础信息
PHAssetResource * resource = [[PHAssetResource assetResourcesForAsset: PHAsset] firstObject];
NSLog(@"%@",resource);
/*
type:
originalFilename:
*/

PHAsset - 获取视频数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
options.version = PHImageRequestOptionsVersionCurrent;
options.deliveryMode = PHVideoRequestOptionsDeliveryModeAutomatic;
PHImageManager *manager = [PHImageManager defaultManager];
[manager requestAVAssetForVideo:asset
options:options
resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
// asset 类型为 AVURLAsset 为此资源的fileURL
// <AVURLAsset: 0x283386e60, URL = file:///var/mobile/Media/DCIM/100APPLE/IMG_0049.MOV>
AVURLAsset *urlAsset = (AVURLAsset *)asset;
// 视频数据
NSData *vedioData = [NSData dataWithContentsOfURL:urlAsset.URL];
NSLog(@"%@",vedioData);
}];

Demo

建议使用《TZImagePickerControllerDemo》来获取视频和图片。

相关链接