目前我知道有两种方式
第一种比较简单
直接把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;
}