路径的拼接问题
1. stringByAppendingString (是在后面加后缀的意思)
在模拟器上好用,真机上提示无法进行操作,改成第二种写法即可.
2.stringByAppendingPathComponent(是在后面加上“/”号连接 让它成为完整的路径)
附上下载代码:(协议可以改成block)
// 1. 建立请求
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:model.music_url]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
// 下载 存储路径
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingString:model.path];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];
// 设置下载进程块代码
/*
bytesRead 当前一次读取的字节数(100k)
totalBytesRead 已经下载的字节数(4.9M)
totalBytesExpectedToRead 文件总大小(5M)
*/
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
// 设置进度条的百分比
CGFloat precent = (CGFloat)totalBytesRead / totalBytesExpectedToRead;
// NSLog(@"%f", precent);
if ([self.delegate respondsToSelector:@selector(downloadMusicProgress:)]) {
[self.delegate downloadMusicProgress:precent];
}
}];
// 设置下载完成操作
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
if ([self.delegate respondsToSelector:@selector(downloadMusic:success:)]) {
[self.delegate downloadMusic:model success:nil];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if ([self.delegate respondsToSelector:@selector(downloadMusic:error:)]) {
[self.delegate downloadMusic:model error:error];
}
}];
NSLog(@"加入队列");
[_downloadQueue addOperation:operation];