-->

MBProgressHUD与NSURLConnection的(MBProgressHUD with

2019-06-27 17:27发布

我试图用MBProgressHUD与NSURLConnection的。

在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];
}

运行它时,HUD确定模式旋转的罚款。

我试图实现这一点,但在这里

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        currentLength += [data length];
        HUD.progress = currentLength / (float)expectedLength;
    }

圈子是空的,没有填充。

我不知道是否这取决于请求的URL的尺寸。

我请求让我从我的网站下载的plist(〜80 KB),但圈内一直是空的,控制台报告

<Error>: void CGPathAddArc(CGPath*, const CGAffineTransform*, CGFloat, CGFloat, CGFloat, CGFloat, CGFloat, bool): invalid value for start or end angle.

我甚至尝试做到这一点的方法:

float progress = 0.0f;
    while (progress < 1.0f) {
        progress += 0.01f;
        HUD.progress = progress;
    }

但是,现在这个循环是完全充分,没有做任何的动画。

我认为这取决于所请求的URL的尺寸,但我不那么肯定,没有人知道如何解决这一问题?

Answer 1:

您应该检查的价值[response expectedContentLength]didReceiveResponse

HTTP服务器可以省略“内容长度”报头,并使用“传输编码:分块”代替。 在这种情况下,内容长度不先验已知的和[response expectedContentLength]返回NSURLResponseUnknownLength (其为-1 )`。

我能想象,设置HUD.progress为负值导致CGPathAddArc控制台消息。

根据该文件,它也可能发生累积currentLength变得比预期的响应长度大,所以你应该检查之谓也。



Answer 2:

我的问题解决了这种方式,从切换NSURLRequestNSMutableURLRequest和值没有设定到编码(先前在gzip的)

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:anURL];
[request addValue:@"" forHTTPHeaderField:@"Accept-Encoding"];


文章来源: MBProgressHUD with NSURLConnection