沙盒中的文件夹
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];