带有NSURLConnection的MBProgressHUD [英] MBProgressHUD with NSURLConnection

查看:82
本文介绍了带有NSURLConnection的MBProgressHUD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将MBProgressHUD与NSURLConnection一起使用.

I was trying to use MBProgressHUD with NSURLConnection.

MBProgressHUD报告的演示项目中的示例:

The example in Demo project of MBProgressHUD reports:

- (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旋转得很好.

Running it, the HUD in determinate mode spins fine.

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

I tried to implement this, but here

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

圆圈是空的,没有填充.

the circle is empty and not filled.

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

I don't know if it depends on the dimension of the requested url.

我请求从我的网站下载plist(〜80 kb),但圈子一直空白,控制台报告

I request to download a plist (~80 kb) from my website, but the circle keeps being empty and console reports

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

但是现在圆圈完全充满了,没有做任何动画.

But now the circle is completely full and not doing any animation.

我认为这取决于所请求网址的尺寸,但是我不确定,有人知道如何解决此问题吗?

I think it depends on the dimension of the requested url, but i'm not so sure, does anyone know how to fix this?

推荐答案

我以这种方式解决了这个问题,从NSURLRequest切换到NSMutableURLRequest,并将值none设置为编码(以前在gzip中使用)

I solved the problem this way, switching from NSURLRequest to NSMutableURLRequestand setting the value none to the encoding (previously in gzip)

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

这篇关于带有NSURLConnection的MBProgressHUD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆