iOS获取本地视频列表数据

iOS本地的视频存在于三个地方:

  • iTunes中影片中导入的视频文件
  • iTunes中通过共享文件导入的视频文件,即应用沙盒
  • 摄像头拍摄的视频文件,可以相册找到

这三处获取视频列表方式不同。

iTunes中影片中导入的视频文件

需要导入头文件:

	#import 

查询代码:

	// iTunes check
    MPMediaQuery *query = [[MPMediaQuery alloc] init];
    MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithInt:MPMediaTypeAnyVideo] forProperty:MPMediaItemPropertyMediaType];
    [query addFilterPredicate:predicate];
    NSArray *itemList = [query items];
    for (MPMediaItem *item in itemList) {
        NSLog(@"item title string: %@", item.title);
        NSLog(@"item url string: %@", item.assetURL.absoluteString);
    }

此处获取到的视频文件路径为URL,可直接使用系统播放器播放此URL:
eg: ipod-library://item/item.mp4?id=3219156422165626236
eg: ipod-library://item/item.mp4?id=8296471224336825272

iTunes中通过共享文件导入到应用沙盒的视频文件

查询代码:

 	NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:docDir error:nil];
    for (int i = 0; i < fileList.count; i++) {
        NSString *path = fileList[i];
        NSLog(@"Path: %@", path);
    }

此处获取到视频文件的绝对路径。
eg. /var/mobile/Containers/Data/Application/D335CF8E-8D73-4F38-8309-4C1DC5048188/Documents/2.2.5.mp4

摄像头拍摄的视频文件,可以相册找到

需要导入头文件:

#import 

查询代码:

PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:[self buildPhotosOptions]];
NSMutableArray *sourceArray = [NSMutableArray arrayWithCapacity:result.count];
for (PHAsset *assets in result) {
    NSArray *assetResources = [PHAssetResource assetResourcesForAsset:assets];
    PHAssetResource *assetRes = [assetResources firstObject];
    NSLog(@"originalFilename %@", assetRes.originalFilename);
    NSLog(@"uniformTypeIdentifier %@", assetRes.uniformTypeIdentifier);
    NSLog(@"assetLocalIdentifier %@", assetRes.assetLocalIdentifier);
}

可使用UIImagePickerController访问数据

你可能感兴趣的:(ios)