MBProgressHud和SDWebImagePrefetcher

我试图显示一个自定义的MBProgressHUD同时使用NSURLConnection方法下载MBProgressHUD的URL列表。

SDWebImagePrefetcher有一个方法,当被调用时,在控制台中显示图像下载的进度。

现在,我想展示NSLog在自定义MBProgressHUD中的MBProgressHUD ,我希望HUD能够保持在屏幕上,直到完成这个过程,但是我不知道该怎么做,再加NSURLConnection我的NSURLConnection方法被调用时,它显示最初的HUD(连接),然后快速跳转到“完成”,即使图像仍然需要下载。

这是我的代码:

 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { HUD.mode = MBProgressHUDModeDeterminate; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { HUD.labelText = @"Loading"; HUD.detailsLabelText = @"Downloading contents..."; //here, i would like to show the progress of the download, but it seems to jump this part HUD.dimBackground = YES; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { //arr = array which holds a plist NSMutableArray *array = [[[NSMutableArray alloc]init]autorelease]; for (NSDictionary *dict in arr) { for (NSDictionary *val in [dict valueForKey:STR_ROWS]) { [array addObject:[val objectForKey:@"image"]]; } } [prefetcher prefetchURLs:array]; HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkmark.png"]] autorelease]; HUD.mode = MBProgressHUDModeCustomView; HUD.labelText = NSLocalizedString(@"Completed",@"Completed!"); HUD.detailsLabelText = nil; HUD.dimBackground = YES; [HUD hide:YES afterDelay:2]; } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [HUD hide:YES]; UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"Connection Failed message:[NSString stringWithFormat:@"Connection to the remote server failed with error:\n %@\n Try again in a while"),[error localizedDescription]] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease]; [alertView show]; } 

我试图看看这些例子,但没有find如何去做我想做的事情。

编辑

HUD设置:

 -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ if (buttonIndex == 0){ [alertView dismissWithClickedButtonIndex:0 animated:YES]; } else{ NetworkStatus internetStatus = [internetReachable currentReachabilityStatus]; switch (internetStatus) { case NotReachable: { //not reachable break; } case (ReachableViaWWAN): { //reachable but not with the needed mode break; } case (ReachableViaWiFi):{ HUD = [[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES]retain]; HUD.delegate = self; HUD.dimBackground = YES; HUD.labelText = @"Connecting..."; NSURL *URL = [NSURL URLWithString:@"http://mywebsite.com/myPlist.plist"]; NSURLRequest *request = [NSURLRequest requestWithURL:URL]; NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; [connection start]; [connection release]; break; } default: break; } } } 

有任何想法吗?

connection:didReceiveResponse:你必须logging下载的大小,例如self.responseSize。 然后,在connection:didReceiveData:你必须将你刚刚得到的数据附加到你以前得到的数据,并更新进度:

 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { HUD.mode = MBProgressHUDModeDeterminate; HUD.labelText = @"Loading"; HUD.detailsLabelText = @"Downloading contents..."; HUD.dimBackground = YES; // Define responseSize somewhere... responseSize = [response expectedContentLength]; myData = [NSMutableData data]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [myData appendData:data]; HUD.progress = (float)myData.length / responseSize; }