设置下载NSData的进度条

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文件中

 @interface YourClass : YourSuperclass<NSURLConnectionDataDelegate> 

在你的.m文件中做

 @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]; 

并且还实现委托方法

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

使用该方法无法取得进度callback。

你需要使用NSURLConnectionNSURLConnectionDataDelegate

然后NSURLConnectionasynchronous运行,并将callback发送给它的委托。

主要看的是…

 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response; - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error; - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data; 

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection; 

这些都是用来使连接做你已经做的事情。

编辑

其实,看Marc的回答如下。 它是正确的。

您可以使用MBProgress Hud类来加载视图。 你只能从这里下载两个类: – https://github.com/jdg/MBProgressHUD在你想要加载数据的类中编写代码之后例子:在你的viewDidLoad中,你写这&#x4E2A;

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