Ios 设置下载NSData的进度条

Ios 设置下载NSData的进度条,ios,xcode4.5,Ios,Xcode4.5,我想在下载时设置进度条 使用该方法无法获取进度回调 您需要使用NSURLConnection和NSURLConnectionDataDelegate 然后,NSURLConnection异步运行,并向其委托发送回调 主要要看的是 NSURL *url = [NSURL URLWithString:@"http://i0.kym-cdn.com/entries/icons/original/000/005/545/OpoQQ.jpg?1302279173"]; NSData *data = [N

我想在下载时设置进度条

使用该方法无法获取进度回调

您需要使用
NSURLConnection
NSURLConnectionDataDelegate

然后,
NSURLConnection
异步运行,并向其委托发送回调

主要要看的是

NSURL *url = [NSURL URLWithString:@"http://i0.kym-cdn.com/entries/icons/original/000/005/545/OpoQQ.jpg?1302279173"]; 
NSData *data = [NSData dataWithContentsOfURL:url]; 
imageView.image = [[[UIImage imageWithData:data];

这些都是用来让连接做你已经在做的事情

编辑


事实上,见下面马克的答案。这是正确的。

请给出更详细的示例:

在您的.h文件中

- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
找个地方打电话

@property (nonatomic) NSMutableData *imageData;
@property (nonatomic) NSUInteger totalBytes;
@property (nonatomic) NSUInteger receivedBytes;
并实现了委托方法

NSURL *url = [NSURL URLWithString:@"http://i0.kym-cdn.com/entries/icons/original/000/005/545/OpoQQ.jpg?1302279173"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

可以使用MBProgress Hud类加载视图。您只能从此处下载两个类:- 在您要加载数据的类中编写此代码之后 示例:在viewDidLoad中,您编写了以下内容

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) urlResponse;
    NSDictionary *dict = httpResponse.allHeaderFields;
    NSString *lengthString = [dict valueForKey:@"Content-Length"];
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    NSNumber *length = [formatter numberFromString:lengthString];
    self.totalBytes = length.unsignedIntegerValue;

    self.imageData = [[NSMutableData alloc] initWithCapacity:self.totalBytes];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.imageData appendData:data];
    self.receivedBytes += data.length;

    // Actual progress is self.receivedBytes / self.totalBytes
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    imageView.image = [UIImage imageWithData:self.imageData];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    //handle error
}

您可以使用
AFNetworking
,但是如果您不想要第三方库,这个问题的@richy答案可能会有所帮助。只有在后台线程中运行上述代码,并且在后台线程中使用计时器定期检查NSData长度并更新主线程上的进度条值,您才可以显示上述代码的进度条(仅当您事先知道NSData长度时才有效,否则您必须使用异步NSURLconnection来获得确切的文件大小)。不确定反对票是什么?顺便说一句,我建议避免使用第三方框架,如AFNetworking。至少在您能够自如地编写自己的网络之前。即使如此,我也避免使用它们。我更喜欢调试自己的代码,而不是其他人的代码。@Dinesh他如何获得预期的数据长度,以便设置完成百分比关于?@Fogmeister更新了我的评论&谢谢你指出这一点……或者他可以将下载放入
dispatch\u async
块,并在其中调用下载;>是的,但他不会从中得到实际进展。他只能知道何时完成。对,他想要的是一个酒吧,而不仅仅是一个纺车。@Fogmeister你没有使用correct方法。您应该使用NSURLConnectionDownloadDelegate来报告进度。这就是它的用途。请参阅我的要点。我从工作代码中提取了此方法。我想这些是正确的方法。我想您可以使用该方法。正确的方法是使用下载代理。老实说,我不这么认为。下载代理用于NSURLConnections,它是由Newstand Kit创建的。在我的(和你的)中实现时,NSURLConnection是手动创建的,因此数据委托更适合。啊,也许你是对的。不知道。我以前用过这个,但错过了报摊。实际上你是对的。这只会显示微调器,而不是进度条。你也可以通过简单的活动指示器视图来完成
NSURL *url = [NSURL URLWithString:@"http://i0.kym-cdn.com/entries/icons/original/000/005/545/OpoQQ.jpg?1302279173"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) urlResponse;
    NSDictionary *dict = httpResponse.allHeaderFields;
    NSString *lengthString = [dict valueForKey:@"Content-Length"];
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    NSNumber *length = [formatter numberFromString:lengthString];
    self.totalBytes = length.unsignedIntegerValue;

    self.imageData = [[NSMutableData alloc] initWithCapacity:self.totalBytes];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.imageData appendData:data];
    self.receivedBytes += data.length;

    // Actual progress is self.receivedBytes / self.totalBytes
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    imageView.image = [UIImage imageWithData:self.imageData];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    //handle error
}
- (void) viewDidLoad
{
    MBProgressHud *spinner =  [MBProgressHUD showHUDAddedTo:self.view animated:YES];

        spinner.mode = MBProgressHUDModeCustomView;

        [spinner setLabelText:@"Loading....."];

        [spinner setLabelFont:[UIFont systemFontOfSize:15]];

        [spinner show:YES];

        [self performSelectorInBackground:@selector(getData) withObject:nil];
}

- (void) getData
{
     NSURL *url = [NSURL URLWithString:@"http://i0.kym-cdn.com/entries/icons/original/000/005/545/OpoQQ.jpg?1302279173"]; 

        NSData *data = [NSData dataWithContentsOfURL:url]; 

        imageView.image = [[[UIImage imageWithData:data];

        [spinner hide:YES];

        [spinner removeFromSuperViewOnHide];
}