下载网络视频到手机相册
2014-09-05 16:54
316 查看
目前我知道有两种方式
第一种比较简单
直接把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;
}
第一种比较简单
直接把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;
}
相关文章推荐
- Swift 4.0 + 将下载的网络视频保存到相册
- android开发,APP下载成功的图片或者视频同时显示在手机自带的相册里
- 下载视频并保存至手机相册
- php视频教程打包下载 - 网络上最好的php视频教程
- 社交网络、手机视频迅速崛起 将再造互联网
- 网络设备基础防护----视频下载
- Revit 2013 API新功能讲座--免费网络视频讲座5月31日下午2:00 (已结束,等待录制的视频下载)
- 3G手机视频网络直播
- 蛙蛙推荐:微软网络讲座系列教程视频下载(2004年1月到2005年4越)
- 企业网络安全基本方案----视频下载
- NFS网络文件系统的快速配置----视频下载
- Android手机开发:网络连接-打开Url下载信息
- 社交网络、手机视频迅速崛起 将再造互联网
- 免费网络远程视频会议系统,免费美女多人视频聊天(附源码下载)(Flex和Fms3开发)
- ITAA:网络模拟平台+学习资料+学习视频=7.5G(Bit下载)
- 企业网络面临的威胁----视频下载
- 社交网络、手机视频迅速崛起 将再造互联网
- 这里有一些常用的学习视频的下载,有学习网络的朋友可以参考
- php视频教程打包下载 - 网络上最好的php视频教程
- 基于Java的简单网络爬虫的实现--下载Silverlight视频