1

我的应用程序中有一个 mp4 视频(在 Document 文件夹中)!我想将其复制或移动到 Photolibrary... 我该怎么做?请帮我!

4

3 回答 3

1

如果你不想使用 assest 库,这里是代码

 NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
 NSString *videoFile = [documentDirectory stringByAppendingPathComponent:@"video.mp4"];
 UISaveVideoAtPathToSavedPhotosAlbum(videoFile, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);


- (void)video:(NSString *) videoPath didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo {
     if(error)
           NSLog(@\"didFinishSavingWithError: %@\", error);
}
于 2013-02-20T09:44:33.590 回答
0

我认为你可以使用:

- (void)writeVideoAtPathToSavedPhotosAlbum:(NSURL *)videoPathURL completionBlock:(ALAssetsLibraryWriteVideoCompletionBlock)completionBlock

方法从ALAssetsLibrary

于 2013-02-20T09:38:30.220 回答
0

要移动到照片库(在 iOS 中称为相机胶卷):

NSString *fileURLString = @"file://....somepath.../somefile.mp4";
NSURL *fileURL = [NSURL URLWithString:fileURLString];

// Check compatibility
if ([[ALAssetsLibrary new] videoAtPathIsCompatibleWithSavedPhotosAlbum:fileURL]) {
    // Save to Photo Library / Camera Roll
    [[ALAssetsLibrary new] writeVideoAtPathToSavedPhotosAlbum:fileURL completionBlock:nil];
} else {
    NSLog(@"Incompatible type. Cannot save to Photo Library.");
}

要复制到任何文件夹,但您可能不想这样做:

NSFileManager *fileManager = [NSFilemanager defaultManager];
NSURL *originalDirectory = [NSURL fileURLWithPath:@"/originalDir/filename.mp4"];
NSURL *newDirectory = [NSURL fileURLWithPath:@"/newDirectory/filename.mp4"];

[fileManager moveItemAtURL:originalDirectory toURL:newDirectory error:nil];

如果您需要先下载,请执行以下操作:

使用ALAssetsLibrary如下:

  #import <AssetsLibrary/AssetsLibrary.h>
  // ...
  ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
  [assetsLibrary writeVideoAtPathToSavedPhotosAlbum:yourVideoURL completionBlock:^(NSURL *assetURL, NSError *error){
    if (error) {
      // Do something, like show an Alert View describing the error
    }
  }];
于 2015-01-07T22:01:32.673 回答