AFNetworking下载网络相关的附件(音视频)等到本地app包中做离线来提高用户体验
2015-07-22 15:44
866 查看
使用场景:
有时我们做的app在请求网络相关的附件资源时有图片的也有音视频的,请求图片我们常用的第三方库一般是
SDWebImage就足够了,但SDWebImage不支持对像音频、视频及压缩zip等附件的下载支持,这时我们可以利用
AFNetworking的AFHTTPRequestOperation类来完成下载并保存到指定的app的Caches目录下,底层对这块的封装
代码如下:
.h头文件接口
[objc] view
plaincopy
/**
* 下载文件
*
* @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;
.m实现代码如下:
[objc] view
plaincopy
/**
* 下载文件
*/
- (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 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) {
//下载失败
[self requestFailed:aTag];
}];
[operation start];
}
}
有时我们做的app在请求网络相关的附件资源时有图片的也有音视频的,请求图片我们常用的第三方库一般是
SDWebImage就足够了,但SDWebImage不支持对像音频、视频及压缩zip等附件的下载支持,这时我们可以利用
AFNetworking的AFHTTPRequestOperation类来完成下载并保存到指定的app的Caches目录下,底层对这块的封装
代码如下:
.h头文件接口
[objc] view
plaincopy
/**
* 下载文件
*
* @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;
.m实现代码如下:
[objc] view
plaincopy
/**
* 下载文件
*/
- (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 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) {
//下载失败
[self requestFailed:aTag];
}];
[operation start];
}
}
相关文章推荐
- 网络扫描工具nc&nmap
- http状态码 200 304 404 503等
- IP、ICMP、UDP、TCP 校验和算法
- TCP状态变迁图
- 使用java读取url网络文件
- windows编程--网络编程学习--winsock编程(2)
- Android访问网络 使用HttpURLConnection还是HttpClient?
- Andorid Volley网络请求详解
- 计算机网络层介绍
- Can not find the tag library descriptor for "http://struts.apache.org/tags-bean
- TCP/IP详解学习笔记(13)-TCP坚持定时器,TCP保活定时器
- TCP/IP详解学习笔记(12)-TCP的超时与重传
- 简单的http客户端
- UNIX网络编程卷一:第二章 传输层:TCP、UDP
- TCP/IP详解学习笔记(11)-TCP交互数据流,成块数据流
- TCP/IP详解学习笔记(10)-TCP连接的建立与中止
- TCP/IP详解学习笔记(9)-TCP协议概述
- TCP/IP详解学习笔记(8)-DNS域名系统
- TCP/IP详解学习笔记(7)-广播和多播,IGMP协议
- 黑马程序员——java基础——Socket网络编程