Add NSURLConnection loading process on UIProgressView

前端 未结 1 1305
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-05 08:40

I created a UIProgressView. But i used NSTimer to UIProgressView\'s process . Now I need to integrate UIProgressView pro

相关标签:
1条回答
  • 2020-12-05 09:44

    In your didReceiveResponse function you could get the total filesize like so: _totalFileSize = response.expectedContentLength;.

    In your didReceiveData function you can then add up to a total bytes received counter: _receivedDataBytes += [data length];

    Now in order to set the progressbar to the correct size you can simply do: MyProgressBar.progress = _receivedDataBytes / (float)_totalFileSize

    (either in the didReceiveData function or somewhere else in your code)

    Don't forget to add the variables that hold the number of bytes to your class!

    I hope this helps..

    EDIT: Here's how you could implement the delegates in order to update progressview

    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
        _totalFileSize = response.expectedContentLength;
        responseData = [[NSMutableData alloc] init];
    }
    
    
    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
       _receivedDataBytes += [data length];
       MyProgressBar.progress = _receivedDataBytes / (float)_totalFileSize;
       [responseData appendData:data];
     }
    
    0 讨论(0)
提交回复
热议问题