【问题标题】:Add NSURLConnection loading process on UIProgressView在 UIProgressView 上添加 NSURLConnection 加载过程
【发布时间】:2010-11-23 10:51:20
【问题描述】:

我创建了一个UIProgressView。但我使用NSTimerUIProgressView's 进程。现在我需要在加载 URL 时集成UIProgressView 进程。 UIProgressView's 的大小将取决于 NSURLConnection's 数据。

我用下面的代码NSURLConnection

-(void)load {
    NSURL *myURL = [NSURL URLWithString:@"http://feeds.epicurious.com/newrecipes"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL
                                                           cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                       timeoutInterval:60];

    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    responseData = [[NSMutableData alloc] init];
}


-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [connection release];

    UIAlertView *alert = [[UIAlertView alloc] init];
    [alert setTitle:@"Warning"];
    [alert setMessage:@"Network Connection Failed?"];
    [alert setDelegate:self];
    [alert addButtonWithTitle:@"Yes"];

    [alert show];
    [alert release];

    NSLog(@"Error");
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
    responsetext = [[[NSString alloc] initWithData:responseData encoding: NSASCIIStringEncoding] autorelease];
}

【问题讨论】:

    标签: ios nsurlconnection uiprogressview


    【解决方案1】:

    在您的 didReceiveResponse 函数中,您可以获得总文件大小,如下所示: _totalFileSize = response.expectedContentLength;.

    然后,在您的 didReceiveData 函数中,您可以将接收到的总字节数计数器相加: _receivedDataBytes += [data length];

    现在,为了将进度条设置为正确的大小,您只需执行以下操作: MyProgressBar.progress = _receivedDataBytes / (float)_totalFileSize

    (在 didReceiveData 函数或代码中的其他位置)

    不要忘记将保存字节数的变量添加到您的类中!

    我希望这会有所帮助..

    编辑:以下是您如何实现委托以更新进度视图

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

    【讨论】:

    • 如何在 didReceiveResponse 中声明响应,请编辑以下代码。 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { totalFileSize=response.expectedContentLength; receivedDataBytes += [数据长度]; NSLog(@"totalFileSize / totalFileSize = %f",receivedDataBytes / totalFileSize); [响应数据追加数据:数据]; }
    • didReceiveResponse 和 didReceiveData 是 2 个不同的代表。它应该是: - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { totalFileSize=response.expectedContentLength;并从 didReceiveData 中删除该行
    • 编辑了我的答案。我忘了提到你划分的值之一应该被转换为浮点数。因为否则你可能会得到一个整数,没有浮点精度。
    • B_vdl :感谢您的回答。我的问题解决了。但我编辑了一些代码。请检查并告诉您的命令...
    • - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { totalFileSize = response.expectedContentLength; responseData = [[NSMutableData alloc] init]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { receivedDataBytes += [数据长度]; NSLog(@"receivedDataBytes / (float)totalFileSize; = %2f",(1+((float)totalFileSize / (float)receivedDataBytes)*1000)); // MyProgressBar.progress = receivedDataBytes / (float)totalFileSize; [响应数据追加数据:数据]; }
    猜你喜欢
    • 1970-01-01
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    • 1970-01-01
    • 2010-09-23
    相关资源
    最近更新 更多