如何使用ALAssetsLibrary仅获取video
我试图从下面的代码片段中获取照片库中的video。但我也得到图像列表。如何获得所有video的列表? 我究竟做错了什么?
NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init]; xy =[[NSMutableArray alloc]init]; ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) { if(result != nil) { if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) { [assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]]; NSLog(@"result is:%@",result); NSLog(@"asset URLDictionary is:%@",assetURLDictionaries); NSURL *url= (NSURL*) [[result defaultRepresentation]url]; [library assetForURL:url resultBlock:^(ALAsset *asset) { [xy addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]]; NSLog(@" xy is:%@",xy); image =[ [UIImageView alloc ] initWithImage:[xy objectAtIndex:0]]; NSLog(@"image is:%@",image); } failureBlock:^(NSError *error){ NSLog(@"test:Fail"); } ]; } } }; NSMutableArray *assetGroups = [[NSMutableArray alloc] init]; void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) { NSLog(@"hello"); if(group != nil) { [group enumerateAssetsUsingBlock:assetEnumerator]; [assetGroups addObject:group]; NSLog(@"Number of assets in group :%d",[group numberOfAssets]); NSLog(@"asset group is:%@",assetGroups); } }; assetGroups = [[NSMutableArray alloc] init]; [library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:assetGroupEnumerator failureBlock:^(NSError *error) {NSLog(@"A problem occurred");}];
您需要在枚举期间向组中添加ALAssetsFilter。 这是一个基本的例子:
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init]; [library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) { if (group) { [group setAssetsFilter:[ALAssetsFilter allVideos]]; [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop){ if (asset){ NSDictionary *meta = [[asset defaultRepresentation] metadata]; } }]; } } failureBlock:^(NSError *error) { NSLog(@"error enumerating AssetLibrary groups %@\n", error); }];
为了将来参考, 可用的filter是:
- allPhotos - allVideos - allAssets
- React Native IOS 11.3 – 11.4错误:JavaScriptCore JSC :: JSLock :: DropAllLocks :: DropAllLocks
- 核心数据NSFetchRequest setResultType:NSDictionaryResultType不起作用
- 应用内购买:视图消失时崩溃
- iOS – 在相机上定位自定义覆盖(垂直alignment)。 顶部黑色栏的大小
- iOS GLSL。 有没有办法使用GLSL着色器创build图像直方图?
- 你好! 我是亚历克西斯。 –亚历克西斯·埃利奥特(Alexis Elliott)
- TWTRTwitter sessionStore现在返回TWTRAuthSession:那么现在如何访问userName属性?
- iOS SDK:通过代码将button移动到屏幕中央
- 在UITableViewCell iOS 8(Swift)中隐藏对象