MBProgressHud and SDWebImagePrefetcher

I am trying to show a custom MBProgressHUD when loading a list of URLs from SDWebImagePrefetcher using NSURLConnection methods.

SDWebImagePrefetcher has a method that, when called, shows in the console the progress of loading images.

Now I would like to show that NSLog progressing in the user MBProgressHUD , and I would like the HUD to remain on the screen until the process is completed, but I don’t know how to do this and plus when my NSURLConnection methods NSURLConnection called, it shows the initial HUD (Connnecting), and then quickly goes to “Complete”, even if the images still need to be downloaded.

Here is my code:

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

I tried to learn examples, but did not know how to do what I want to do.

EDIT

HUD setup:

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

Any ideas?

+4
source share
1 answer

In connection:didReceiveResponse: you should write how large the load is, for example, in self.responseSize. Then in connection:didReceiveData: you should add the data that you just received to the previously received data and update the progress:

 - (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; } 
+4
source

Source: https://habr.com/ru/post/1434384/


All Articles