下载网络视频到手机相册

目前我知道有两种方式 


第一种比较简单 

直接把URL 写入文件  再通过 ALAssetsLibrary 保存到相册中


- (void)videoPlay{
    [musicBt startSpin];
    NSString *urlString = [NSString stringWithFormat:@"http://192.168.1.10/shenxu/2.mp4"];
    NSArray *urlStringArray = [urlString componentsSeparatedByString:@"/"];
    NSString *fileName = [urlStringArray lastObject];
    NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filePath = [NSString stringWithFormat:@"%@/%@", docDirPath,fileName];
    NSError *error;
    NSFileManager *manager = [NSFileManager defaultManager];
    BOOL writeSuc = [manager fileExistsAtPath:filePath];
    NSData *audioData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
    if (writeSuc) {
        [manager removeItemAtPath:filePath error:nil];
    }
    if ([audioData writeToFile:filePath atomically:YES]) {
        NSLog(@"成功");
    } else {
        NSLog(@"失败");
    }
    [self save:filePath];

}

- (void)save:(NSString*)urlString{
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:urlString]
                                completionBlock:^(NSURL *assetURL, NSError *error) {
                                     if (error) {
                                        NSLog(@"Save video fail:%@",error);
                                    } else {
                                        NSLog(@"Save video succeed.");
                                        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"下载完成" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
                                        [alert show];
                                        [musicBt stopSpin];
                                    }
                                }];
}





第二种方式是通过asihttp.....来下载视频到本地   再通过下载的地址string 传到ALAssetsLibrary 相册里

- (void)download{
    NSString *webPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Private Documents/Temp"];
    NSString *cachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Private Documents/Cache"];
     NSFileManager *fileManager = [NSFileManager defaultManager];
    if(![fileManager fileExistsAtPath:cachePath]){
        [fileManager createDirectoryAtPath:cachePath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    ASIHTTPRequest *request=[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://192.168.1.10/shenxu/2.mp4"]];
    //下载完存储目录
    [request setDownloadDestinationPath:[cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"vedio.mp4"]]];
    //临时存储目录
    [request setTemporaryFileDownloadPath:[webPath stringByAppendingPathComponent:[NSString stringWithFormat:@"vedio.mp4"]]];
    [request setDownloadProgressDelegate:self];  //加上去 把进度打印出来

    //断点续载
    [request setAllowResumeForFileDownloads:YES];
    [request startAsynchronous];
    videoRequest = request;
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:request.downloadDestinationPath]
                                completionBlock:^(NSURL *assetURL, NSError *error) {
                                    if (error) {
                                        NSLog(@"Save video fail:%@",error);
                                    } else {
                                        NSLog(@"Save video succeed.");
                                        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"下载完成" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
                                        [alert show];
//                                        [musicBt stopSpin];
                                    }
                                }];
}

#pragma mark - ASIProgressDelegate

- (void)setProgress:(float)newProgress{
    NSLog(@"----- %f ------",newProgress);
    
}

其中需要导入的文件有:Reachability2  ASIHTTPRequest  HttpServer



appdelegate中要写   - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    
    [DDLog addLogger:[DDTTYLogger sharedInstance]];
        httpServer = [[HTTPServer alloc] init];
    [httpServer setType:@"_http._tcp."];
    
    [httpServer setPort:12345];
    
    NSString *webPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Private Documents/Temp"];
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if(![fileManager fileExistsAtPath:webPath])
    {
        [fileManager createDirectoryAtPath:webPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    [httpServer setDocumentRoot:webPath];
    
    [self startServer];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:[ViewController alloc]];
    self.window.rootViewController = nav;
    
    [self.window makeKeyAndVisible];
    return YES;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值