【问题标题】:ALAssetsLibrary get all videosALAssetsLibrary 获取所有视频
【发布时间】:2013-12-16 14:27:10
【问题描述】:

我正在制作一个相机应用程序,我想在其中访问用户在其 iPhone 上创建的所有视频。

目前我拥有的代码仅从用户相机胶卷中获取视频。我的一些用户抱怨他们在相册应用程序下创建了多个自定义文件夹,并且他们在其中存储了一些视频。由于我的代码只查看相机胶卷,它不会从其他文件夹中提取电影。我可以访问他们的其他文件夹吗?

这是我目前所拥有的。

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop)
     {
         [group enumerateAssetsUsingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop)
         {
              if (alAsset)
              {
                  ALAssetRepresentation *representation =[alAsset defaultRepresentation];
                  NSURL *url = [representation url];
                  NSString *assetType=[alAsset valueForProperty:ALAssetPropertyType];


                  //videos only
                  if ([assetType isEqualToString:@"ALAssetTypeVideo"])
                  {
                   .....

【问题讨论】:

  • 尝试将此类 assetsGroupType 用作“ALAssetsGroupAll”。

标签: ios iphone objective-c video alassetslibrary


【解决方案1】:

您必须为资产创建一个过滤器,如下所示:

ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *group, BOOL *stop) {  

    ALAssetsFilter *allVideosFilter = [ALAssetsFilter allVideos];
    [group setAssetsFilter:allVideosFilter];
    //...
};

过滤器选项包括: - 所有资产 - 所有视频 - 所有照片

希望对你有帮助

【讨论】:

    【解决方案2】:

    要获取从 iTunes 同步的媒体,您需要使用 ALAssetsGroupLibrary。 Here 您可以找到 ALAssetsGroupType 的所有可能变体。所以换个方式

    [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:...
    

    [library enumerateGroupsWithTypes:(ALAssetsGroupSavedPhotos | ALAssetsGroupLibrary) usingBlock:...
    

    【讨论】:

      【解决方案3】:

      这会检索所有视频,包括用户从 iTunes 同步的任何视频:

      // Enumerate just the photos and videos by using ALAssetsGroupSavedPhotos
      
      [library enumerateGroupsWithTypes:ALAssetsGroupAll | ALAssetsGroupLibrary
                             usingBlock:^(ALAssetsGroup *group, BOOL *stop)
      {
          if (group != nil)
          {
              // Within the group enumeration block, filter to enumerate just videos.
              [group setAssetsFilter:[ALAssetsFilter allVideos]];
              [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                  if (result) {
                      // Do whatever you need to with `result`
                  }
              }];
          } else {
              // If group is nil, we're done enumerating
              // e.g. if you're using a UICollectionView reload it here
              [collectionView reloadData];
          }
      } failureBlock:^(NSError *error) {
          // If the user denied the authorization request
          NSLog(@"Authorization declined");
      }];
      

      注意ALAssetsGroupAll | ALAssetsGroupLibrary

      根据docs ALAssetsGroupAll 是“与除ALAssetsGroupLibrary 之外的所有组类型进行ORing 相同”。因此,我们还添加了ALAssetsGroupLibrary,其中“包括从 iTunes 同步的所有资产”。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-02
        • 2016-05-13
        • 2021-03-14
        • 2015-08-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多