iOS 关于音乐图片下载到本地沙盒的一些事

沙盒中的文件夹


     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的文件夹,代码如下:

    // 创建music文件夹
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *musicPath = [path stringByAppendingPathComponent:@"music"];
    [fileManager createDirectoryAtPath:musicPath withIntermediateDirectories:YES attributes:nil error:nil];


我们要下载一首歌曲,需要下载一个.mp3文件,一张专辑封面,还有歌曲的一些基本信息(歌名,歌手,专辑名等),为了更好的管理,我们要在music文件夹里创建以歌名命名的文件夹,代码如下:

    //每首歌创建一个文件夹(以歌名命名)
    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];














评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值