如何将视频下载到iPhone照片库?

时间:2014-07-28 11:07:07

标签: ios objective-c

我正在尝试将视频从网址下载到我的iPhone设备的相册中。我做了以下代码。它与我的模拟器一起使用但不适用于实际的iOS设备。我用iPhone和iPad mini试过这个。

这是我的代码

//Download method
- (IBAction)methodToDownload:(id)sender
{
     ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
     UISaveVideoAtPathToSavedPhotosAlbum([self   saveVideoToLocal], self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
    library =   nil;
}




- (void)video:(NSString *) videoPath didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
 {
    if(error)
       NSLog(@"didFinishSavingWithError: %@", error);
    else
    {
          UIAlertView *alertView  =   [[UIAlertView    alloc]initWithTitle:@"Download" message:@"Download completed." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
         [alertView   show];

       //Delete File after creation
       NSFileManager *fileManager = [NSFileManager defaultManager];
       NSError *error;
       BOOL success = [fileManager removeItemAtPath:videoPath error:&error];
    if (success) {
        //            UIAlertView *removeSuccessFulAlert=[[UIAlertView alloc]initWithTitle:@"Congratulation:" message:@"Successfully removed" delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
        //            [removeSuccessFulAlert show];
        NSLog(@"Remove Completed");
    }
    else
    {
        NSLog(@"Could not delete file -:%@ ",[error localizedDescription]);
    }
  }
}

  //Save file to locally
  - (NSString *)saveVideoToLocal {
        NSArray       *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString  *documentsDirectory = [paths objectAtIndex:0];
        NSString  *filePath = [NSString stringWithFormat:@"%@/test.%@", documentsDirectory,@"mp4"];
        NSLog(@"filePath %@",filePath);


       //download the file in a seperate thread.
       dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
       NSLog(@"Downloading Started");

       NSURL  *url = [NSURL URLWithString:@"XYZ_MOVIE_URL.mov"];
       NSLog(@"URL is %@",url);
       NSData *urlData = [NSData dataWithContentsOfURL:url];
       if ( urlData )
       {
          //saving is done on main thread
          dispatch_async(dispatch_get_main_queue(), ^{
            [urlData writeToFile:filePath atomically:YES];

         });
      }

 });
 return filePath;
}

1 个答案:

答案 0 :(得分:4)

// I am using simple way to download the video, You can use according to you.
NSURL *url = [NSURL URLWithString:@"yourfileURL"];
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 writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:path] completionBlock:^(NSURL *assetURL, NSError *error) {

    if (error) {
        NSLog(@"%@", error.description);
    }else {
        NSLog(@"Done :)");
    }

}];

注意:不要忘记导入#import <AssetsLibrary/AssetsLibrary.h>