- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask
*)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
float progress = (float)((float)totalBytesWritten /(float)totalBytesExpectedToWrite);
self.progressView.progress = progress;
self.progressLabel.text = [NSString stringWithFormat:@"%.2f%%", progress*100];
}
我正在通过iOS 7中添加的NSURLSession下载文件。到目前为止,一切正常,但是由于某些原因,用户界面未更新。我已经检查了该方法正在被调用,并且我也可以NSLog进度。
也许因为这种方法经常被调用?但是如何更新用户界面,例如,如果有进度条呢?提前致谢 :)
NSURLSession默认在后台线程中运行,因此您需要在主线程上调用UI更新。
dispatch_async(dispatch_get_main_queue(), ^{
// perform on main
float progress = (float)((float)totalBytesWritten /(float)totalBytesExpectedToWrite);
self.progressView.progress = progress;
self.progressLabel.text = [NSString stringWithFormat:@"%.2f%%", progress*100];
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句