2016-12-01 10 views
0

웹 서비스에서 오는 하나의보기에 10 개 이상의 비디오 URL이 있습니다.이 비디오 축소판을 테이블보기에 표시하고 있습니다. 내가 특정 폴더에있는 갤러리에 동영상을 저장하고있어 어디 나는 다음과 같은 코드를 사용하고Objctive C의 Iphone Gallery에있는 Partcular 폴더에 비디오를 저장하는 방법

이 내 코드 ::

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