【问题标题】:how can I copy mp4 file from documents to photolibrary (in iPhone)?如何将 mp4 文件从文档复制到照片库(在 iPhone 中)?
【发布时间】:2013-02-20 09:35:33
【问题描述】:

我的应用程序中有一个 mp4 视频(位于 Document 文件夹中)!我想将其复制或移动到 Photolibrary... 我怎样才能做到这一点?请帮帮我!

【问题讨论】:

    标签: iphone ios objective-c copy photolibrary


    【解决方案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);
    }
    

    【讨论】:

    • UISaveVideoAtPathToSavedPhotosAlbum 对我不起作用:(!而且也没有发生错误......
    • 也可以使用UIVideoAtPathIsCompatibleWithSavedPhotosAlbum函数来检查视频是否兼容。
    【解决方案2】:

    我认为你可以使用:

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

    来自ALAssetsLibrary的方法

    【讨论】:

    • 如何使用 ALAssetsLibrary?
    【解决方案3】:

    要移动到照片库(在 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
        }
      }];
    

    【讨论】:

      猜你喜欢
      • 2018-11-10
      • 1970-01-01
      • 1970-01-01
      • 2021-08-28
      • 1970-01-01
      • 1970-01-01
      • 2011-05-02
      • 2010-11-25
      • 1970-01-01
      相关资源
      最近更新 更多