【问题标题】:How to save the videos in Partcular folder in iPhone Gallery in Objective-C如何在Objective-C中将视频保存在iPhone画廊的特定文件夹中
【发布时间】:2017-04-15 18:18:46
【问题描述】:

我在一个视图中有超过 10 个视频 url,它们来自 Web 服务。我在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
                        }];

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

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

【问题讨论】:

标签: ios objective-c iphone video alassetslibrary


【解决方案1】:
- (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);
            }
        }];

 }

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多