iOS 关于音乐图片下载到本地沙盒的一些事
来源:互联网 发布:cmd mysql 命令 编辑:程序博客网 时间:2024/06/07 06:35
沙盒中的文件夹
1.Documents文件夹:用户要存储的内容都写在这个文件夹里,一般来说是用户对应用程序的设置,比如,夜间模式,一旦app使用了苹果的ICloud服务,app会自动把documents文件夹中的所有内容上传到服务器
2.Library文件夹:
2.1 Caches文件夹:缓存文件夹,所有缓存的文件都推荐写在这里(Json数据,图片,音频,视频。。。)
2.2 Preference文件夹:给开发者使用,保存应用程序的状态和设置
3.tmp文件夹:临时文件夹,保存临时数据
4.应用程序包:里面所有的东西都是只读的,保存的app的所有的代码,资源
NSBundle
获取Document文件夹路径:
//Documents路径 NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
// 创建music文件夹 NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *musicPath = [path stringByAppendingPathComponent:@"music"]; [fileManager createDirectoryAtPath:musicPath withIntermediateDirectories:YES attributes:nil error:nil];
//每首歌创建一个文件夹(以歌名命名) NSString *songPath = [musicPath stringByAppendingPathComponent:songName]; [fileManager createDirectoryAtPath:songPath withIntermediateDirectories:YES attributes:nil error:nil];
把歌的一些基本信息存在一个字典里dic,把字典写入本地:
//字典写入本地(歌名.txt) NSString *dicPath = [songPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt",songName]]; [dic writeToFile:dicPath atomically:YES];
下载音频文件:
//音频下载 [self downloadFileURL:mp3Url savePath:songPath fileName:[NSString stringWithFormat:@"%@.mp3",songName] tag:i];
/** @param string aUrl 请求文件地址* @param string aSavePath 保存地址* @param string aFileName 文件名* @param int aTag tag标识 */- (void)downloadFileURL:(NSString *)aUrl savePath:(NSString *)aSavePath fileName:(NSString *)aFileName tag:(NSInteger)aTag{ NSFileManager *fileManager = [NSFileManager defaultManager]; //检查本地文件是否已存在 NSString *fileName = [NSString stringWithFormat:@"%@/%@", aSavePath, aFileName]; //检查附件是否存在 if ([fileManager fileExistsAtPath:fileName]) { NSData *audioData = [NSData dataWithContentsOfFile:fileName]; [self requestFinished:[NSDictionary dictionaryWithObject:audioData forKey:@"res"] tag:aTag]; }else{ //创建附件存储目录 if (![fileManager fileExistsAtPath:aSavePath]) { [fileManager createDirectoryAtPath:aSavePath withIntermediateDirectories:YES attributes:nil error:nil]; } //下载附件 NSURL *url = [[NSURL alloc] initWithString:aUrl]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; operation.inputStream = [NSInputStream inputStreamWithURL:url]; operation.outputStream = [NSOutputStream outputStreamToFileAtPath:fileName append:NO]; [operation start]; //下载进度控制 /* [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) { NSLog(@"is download:%f", (float)totalBytesRead/totalBytesExpectedToRead); }]; */ //已完成下载 [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSData *audioData = [NSData dataWithContentsOfFile:fileName]; //设置下载数据到res字典对象中并用代理返回下载数据NSData [self requestFinished:[NSDictionary dictionaryWithObject:audioData forKey:@"res"] tag:aTag]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"错误:%@",error); //下载失败 [self requestFailed:aTag]; }]; }}
图片下载,下载到沙盒文件,以.jpg的形式存在:
//图片下载 UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:picUrl]]]; NSString *picUrlPath = [songPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg",songName]]; [UIImagePNGRepresentation(image) writeToFile:picUrlPath atomically:YES];
//document路径 NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; //音频文件路径 NSString *offlineMusicPath = [NSString stringWithFormat:@"%@/music/%@/%@.mp3",path,songName,songName]; //图片文件路径 NSString *picString = [NSString stringWithFormat:@"%@/music/%@/%@.jpg",path,songName,songName]; //音频文件路径写成url NSURL *url = [NSURL fileURLWithPath:offlineMusicPath]; //图片文件路径装换成UIImage UIImage *picImage = [UIImage imageWithContentsOfFile:picString];
0 0
- iOS 关于音乐图片下载到本地沙盒的一些事
- iOS网络图片下载到本地
- iOS 网络请求图片下载到本地相册
- iOS开发------实现图片下载缓存到本地
- ios 图片下载并保存到本地
- 将图片下载到本地
- 网络图片下载到本地相册
- python爬贴吧图片下载到本地
- 点击图片下载到本地的方法总结(ASP)
- 一些图片下载的方法
- 关于获得手机里的本地音乐
- 关于获得手机里的本地音乐
- 关于iOS获取音乐库的音乐信息 (获取本地音乐文件)
- java把远程图片下载到本地
- 使用python将远程图片下载到本地
- PHP 根据URL将图片下载到本地
- php将远程图片下载到本地
- php将远程图片下载保存到本地
- 秒杀多线程第六篇 经典线程同步 事件Event
- Linux C 获取本机IP
- 秒杀多线程第七篇 经典线程同步 互斥量Mutex
- [翻译] 在JAVA中,怎样周期性运行一个任务
- BestCoder Round #43 (hdu5264 - 5266)(数学,LCA)good
- iOS 关于音乐图片下载到本地沙盒的一些事
- 秒杀多线程第八篇 经典线程同步 信号量Semaphore
- Android EditText聚焦时hint消失的简单代码
- 几种基本的pcm编码总结
- 【Android开发】网络编程及Internet应用-通过HTTP访问网络
- 数据库大型应用解决方案总结
- AT&T汇编伪指令
- 系统UIAlertView与代码弹出的区别
- 秒杀多线程第九篇 经典线程同步总结 关键段 事件 互斥量 信号量