Phi*_*lip 3 objective-c nsurlconnection mbprogresshud
我试图使用带有NSURLConnection的MBProgressHUD.
MBProgressHUD演示项目中的示例报告:
- (IBAction)showURL:(id)sender {
NSURL *URL = [NSURL URLWithString:@"https://github.com/matej/MBProgressHUD/zipball/master"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
[connection release];
HUD = [[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES] retain];
HUD.delegate = self;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
expectedLength = [response expectedContentLength];
currentLength = 0;
HUD.mode = MBProgressHUDModeDeterminate;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
currentLength += [data length];
HUD.progress = currentLength / (float)expectedLength;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]] autorelease];
HUD.mode = MBProgressHUDModeCustomView;
[HUD hide:YES afterDelay:2];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[HUD hide:YES];
}
Run Code Online (Sandbox Code Playgroud)
运行它,确定模式下的HUD旋转正常.
我试图实现这个,但在这里
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
currentLength += [data length];
HUD.progress = currentLength / (float)expectedLength;
}
Run Code Online (Sandbox Code Playgroud)
圆圈是空的,没有填充.
我不知道它是否取决于所请求网址的维度.
我请求从我的网站下载一个plist(~80 kb),但是这个圆圈一直是空的并且控制台报告
<Error>: void CGPathAddArc(CGPath*, const CGAffineTransform*, CGFloat, CGFloat, CGFloat, CGFloat, CGFloat, bool): invalid value for start or end angle.
Run Code Online (Sandbox Code Playgroud)
我甚至试图这样做:
float progress = 0.0f;
while (progress < 1.0f) {
progress += 0.01f;
HUD.progress = progress;
}
Run Code Online (Sandbox Code Playgroud)
但现在圆圈已经完全没有做任何动画.
我认为这取决于所请求网址的维度,但我不太确定,有谁知道如何解决这个问题?
我用这种方式解决了这个问题,切换NSURLRequest到NSMutableURLRequest并将值none设置为编码(以前在gzip中)
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:anURL];
[request addValue:@"" forHTTPHeaderField:@"Accept-Encoding"];
Run Code Online (Sandbox Code Playgroud)
你应该检查[response expectedContentLength]in 的值didReceiveResponse.
http服务器可以省略"Content-Length"标头,而是使用"Transfer-Encoding:chunked".在这种情况下,内容长度不是先验已知的并且[response expectedContentLength]返回NSURLResponseUnknownLength(即-1).
我可以想象设置HUD.progress为负值会导致CGPathAddArc控制台消息.
根据文档,也可能发生累积currentLength变得大于预期的响应长度,因此您也应该检查它.
| 归档时间: |
|
| 查看次数: |
3504 次 |
| 最近记录: |