ios - 如何在 Objective-C 中将视频保存在 iPhone Gallery 的特定文件夹中

标签 ios objective-c iphone video alassetslibrary

我在一个 View 中有超过 10 个来自 Web 服务的视频 url。我在 UITableview 中显示这些视频缩略图。 我正在使用以下代码将这些视频保存到特定文件夹中的图库

这是我的代码::

NSURL *url = [NSURL URLWithString:Urlstr];
NSData *data = [NSData dataWithContentsOfURL:url];

// Write it to cache directory
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"file.mov"];
[data writeToFile:path atomically:YES];
// After that use this path to save it to PhotoLibrary
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library addAssetsGroupAlbumWithName:@"MyFolderName" resultBlock:^(ALAssetsGroup *group)
 {
     //How to get the album URL?
     // saving the video in  gallery though file path and document directory
     [library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:path] completionBlock:^(NSURL *assetURL, NSError *error) {

         if (error) {
             NSLog(@"%@", error.description);
         }else {
             NSLog(@"Done :)");
         }
     }];
 }
    failureBlock:^(NSError *error) {
                            //Handle the error
                        }];

现在的问题是它没有将我的视频保存到我的文件夹中。但它会创建文件夹并将视频保存在图库的默认视频文件夹中。

任何人请告诉我为什么会这样。

最佳答案

- (void)createAlbumInPhotosLibrary:(NSString *)photoAlbumName videoAtFile:(NSURL *)videoURL {

     // RELIVIT_moments
     __block PHFetchResult *photosAsset;
     __block PHAssetCollection *collection;
     __block PHObjectPlaceholder *placeholder;

      // Find the album
      PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
      fetchOptions.predicate = [NSPredicate predicateWithFormat:@"title = %@", photoAlbumName];
      collection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum
                                                              subtype:PHAssetCollectionSubtypeAny
                                                              options:fetchOptions].firstObject;
      // Create the album
      if (!collection)
      {
          [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
          PHAssetCollectionChangeRequest *createAlbum = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:photoAlbumName];
          placeholder = [createAlbum placeholderForCreatedAssetCollection];

       } completionHandler:^(BOOL success, NSError *error) {

              if (success)
              {
                 PHFetchResult *collectionFetchResult = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[placeholder.localIdentifier]
                                                                                                                options:nil];
                 collection = collectionFetchResult.firstObject;

                 [self saveVideoInRelivitFolderSetPlaceHolder:placeholder photosAsset:photosAsset collection:collection VideoAtFile:videoURL];

                }

            }];

        } else {

            [self saveVideoInRelivitFolderSetPlaceHolder:placeholder photosAsset:photosAsset collection:collection VideoAtFile:videoURL];
        }

 }

 - (void)saveVideoInRelivitFolderSetPlaceHolder:(PHObjectPlaceholder *)placeholderLocal photosAsset:(PHFetchResult *)photosAssetLocal  collection:(PHAssetCollection *)collectionLocal VideoAtFile:(NSURL *)videoURL {

        __block PHFetchResult *photosAsset = photosAssetLocal;
        __block PHAssetCollection *collection = collectionLocal;
        __block PHObjectPlaceholder *placeholder = placeholderLocal;

        // Save to the album
        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
            PHAssetChangeRequest *assetRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:videoURL];
            placeholder = [assetRequest placeholderForCreatedAsset];
            photosAsset = [PHAsset fetchAssetsInAssetCollection:collection options:nil];

            PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection
                                                                                                                          assets:photosAsset];
            [albumChangeRequest addAssets:@[placeholder]];

        } completionHandler:^(BOOL success, NSError *error) {
            if (success)
            {
                NSLog(@"done");
            }
            else
            {
                NSLog(@"%@", error.localizedDescription);
            }
        }];

 }

关于ios - 如何在 Objective-C 中将视频保存在 iPhone Gallery 的特定文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40905082/

相关文章:

iphone - 录制音频和视频

ios - Realm 列表与 LinkingObjects 性能

ios - 通过 JSON 获取 YouTube 用户的上传总数

objective-c - Swift 应用程序在 Objective C 库中定义的引用类型

iphone - 等待或撤销 iPhone 过期证书?

iphone - 我可以通过浏览器和 JavaScript 以编程方式访问智能手机的传感器吗?

ios - 如何添加指向 map 上图钉的 GPS?

ios - AFNetworking 后台 session KVO 错误

iphone - 逻辑问题,根据数据库条目向 UI 添加按钮

ios - 如何计算两个日期之间的几十年数