如何将下载的视频文件保存到Objective C中的图库中?

时间:2013-08-27 06:20:04

标签: iphone ios objective-c

我想将下载的视频文件存储到图库中,但我无法存储它。请指导我如何解决它?

谢谢,

4 个答案:

答案 0 :(得分:2)

试试这个对我有用

-(void)SaveVideoTOPhotoAlbum
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
        NSString *getImagePath = [basePath stringByAppendingPathComponent:videoName];
           printf(" \n\n\n-Video file == %s--\n\n\n",[getImagePath UTF8String]);
        UISaveVideoAtPathToSavedPhotosAlbum ( getImagePath,self, @selector(video:didFinishSavingWithError: contextInfo:), nil);
    }

//

- (void) video: (NSString *) videoPath didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
    {
        NSLog(@"Finished saving video with error: %@", error);
        //kapil here ****
    }

祝你好运亲爱的

答案 1 :(得分:0)

您可以使用UISaveVideoAtPathToSavedPhotosAlbum

进行保存

解决方案

UISaveVideoAtPathToSavedPhotosAlbum(yourVideoPath, nil, nil, nil);

要获取视频路径,您必须将其保存在任何位置。您可以保存到目标路径。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString * yourVideoPath = [basePath stringByAppendingPathComponent:@"video.mov"];

[yourVideoData writeToFile:yourFilePath atomically:YES];

答案 2 :(得分:0)

请你试试这个link ??

这会将指定路径的影片添加到用户的相机胶卷相册。

void UISaveVideoAtPathToSavedPhotosAlbum (
   NSString  *videoPath,
   id        completionTarget,
   SEL       completionSelector,
   void      *contextInfo
);

您可以在我指定的上述链接中找到进一步的指导

更有用的链接是this

希望它有所帮助..

答案 3 :(得分:0)

试试这个...

NSURL *videoUrl = [NSURL URLWithString:[NSString stringWithFormat:@"Your Video Url"]];

dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(q, ^{

NSData *videoData = [NSData dataWithContentsOfURL:videoUrl];

dispatch_async(dispatch_get_main_queue(), ^{

    // Write it to cache directory
    NSString *videoPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"file.mov"];
    [videoData writeToFile:videoPath atomically:YES];

    // After that use this path to save it to PhotoLibrary
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:videoPath] completionBlock:^(NSURL *assetURL, NSError *error)
     {
         if (error)
         {
             NSLog("Error");
         }
         else
         {
             NSLog("Success");
         }

        }];
    });
});