NSURLConnection带有进度的图片下载

第一步: 继承两个协议


   协议1<NSURLConnectionDelegate>
- (void)connection:connection didFailWithError:error;//连接失败
- (BOOL)connectionShouldUseCredentialStorage:connection;//是否使用证书存储

   协议2<NSURLConnectionDataDelegate>
- (void)connection:connection didReceiveResponse:response;//接收响应
- (void)connection:connection didReceiveData:data;//一点一点接收数据
- (void)connection:connection didFailWithError:error;//下载失败
- (void)connectionDidFinishLoading:connection;//下载完成

第二步:创建4个属性

@property (nona, ) UIProgressView *progessView;//进度条
@property (nona, ) UILabel *progessLabel;//显示进度百分比
@property (nona, ) NSMutableData *downloadData;//保存下载的数据
@property (nona, ) long long allSizeBytes; //文件总大小

第三步:发送请求,开始下载

/*
 *注释:开始下载,根据url地址下载图片
 *
 *
 */
- (void)startDownloadWithUrl:(NSURL *)url
{
    //1.创建请求对象
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    //2.发送异步请求
    [NSURLConnection connectionWithRequest:request delegate:self]; 
    [self initProgessView]; //创建进度条
    [self initProgessLabel]; //创建显示进度百分比
}

代理方法

第四步:接收服务器响应

#pragma mark - NSURLConnectionDelegate代理方法
/**
 *  服务器已经收到响应
 */
- (void)connection:(NSURLConnection *)connection 
didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"收到服务器响应");
    //清空数据
    self.downloadData.length = 0;
    //获取文件总大小
    self.allSizeBytes = response.expectedContentLength;
    //1.文件名字
    NSString *filename = response.suggestedFilename;
    NSLog(@"文件名字:%@",filename);
    //2.文件总大小
    long long fs = response.expectedContentLength;
    NSLog(@"文件总大小:%0.01f MB",fs/1024.0/1024.0);
    //3.文件类型
    NSString *type = response.MIMEType;
    NSLog(@"文件类型:%@",type);
    //4.状态码
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
    NSInteger statusCode = httpResponse.statusCode;
    NSLog(@"状态码:%ld",statusCode);
    //5.服务器响应头信息
    NSDictionary *dic = httpResponse.allHeaderFields;
    NSLog(@"响应头:%@",dic);
}

第五步:接收服务器数据(二进制数据Data,一点一点的)

/**
 *  收到服务器数据了,该方法可能会走多次,服务器会分段把数据传给客户端。
 *  当前数据根据网络环境和硬件性能考虑的,没有版本修改
 */
- (void)connection:(NSURLConnection *)connection
 didReceiveData:(NSData *)data
{
    //追加数据
    [self.downloadData appendData:data];
    //计算进度
    CGFloat progess = (CGFloat)self.downloadData.length /
 self.allSizeBytes;
    //修改进度条
    self.progessView.progress = progess;
    //百分比 %转义是%%
    self.progessLabel.text = 
[NSString stringWithFormat:@"%0.1f%%",progess * 100];
}

第六步:接收服务器数据完成(成功走这个方法)

/**
 *  已经下载完成
 */
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //1.移除控件
    [self.progessLabel removeFromSuperview];
    [self.progessView removeFromSuperview];
    //2.替换图片
    self.image = [UIImage imageWithData:self.downloadData];
}

第六步:接收服务器数据失败(失败走这个方法)

/**
 *  下载失败,一般是网络出问题
 */
- (void)connection:(NSURLConnection *)connection
 didFailWithError:(NSError *)error
{
    NSLog(@"下载失败%@",error);
}

微云盘
带有进度的图片下载demo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 14,018评论 6 13
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,056评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,055评论 25 709
  • 在台湾电视剧“我可能不会爱你”里面 ,我个人非常喜欢程又青这个角色,而且非常羡慕有李大仁这个知己!看这套剧给我最大...
    企鹅的北极星阅读 3,122评论 0 0
  • 今天去面试个培训机构的课程顾问,在聊及该工作具体的工作内容及工作方式的时候,面试官说,工作就是销售课程的,以完成任...
    牌牌阅读 32,883评论 5 5

友情链接更多精彩内容