美文网首页
NSURLConnection带有进度的图片下载

NSURLConnection带有进度的图片下载

作者: Hyman0819 | 来源:发表于2016-10-26 20:04 被阅读12次

    第一步: 继承两个协议

    
       协议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

    相关文章

      网友评论

          本文标题:NSURLConnection带有进度的图片下载

          本文链接:https://www.haomeiwen.com/subject/nnqjuttx.html