2016-12-01 79 views
0

我在一個視圖中超過10個視頻鏈接是從網絡service.I未來我出表考慮到這些視頻的縮略圖。 我使用下面的代碼,我M在特定文件夾中保存這些視頻,畫廊如何在iPhone畫廊Partcular文件夾中的視頻保存在objctiveÇ

這是我的代碼::

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 
         }]; 

現在的問題是它不是救了我的視頻我的文件夾中。但它會創建文件夾並將視頻保存在圖庫的默認視頻文件夾中。 請人建議我爲什麼會這樣。

+0

你爲什麼不使用或得到https://github.com/Kjuly/ALAssetsLibrary-CustomPhotoAlbum – Poles

+0

HII極有一絲..我已經實現了這一點。但是這給了很多錯誤。 – user6898211

+0

你遇到什麼錯誤? – Poles

回答

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); 
      } 
     }]; 

} 
+0

與大賞金類似或更容易的問題.. http://stackoverflow.com/questions/43791151/100-bounty-actually-get-the-filename-of-image-saved-to-photos-album-2017-ios10 – Fattie