MBProgressHud and SDWebImagePrefetcher

青春壹個敷衍的年華 提交于 2019-12-22 08:51:53

问题


I'm trying to show a custom MBProgressHUD while downloading a list of URLs with SDWebImagePrefetcher using NSURLConnection methods.

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

Now, i would like to show that NSLog progress in the custom MBProgressHUD and I would like the HUD to stay on screen until the process is done, but I don't know how to do it and plus, when my NSURLConnection methods are called, it shows the initial HUD (Connnecting), then quickly jumps to "Complete", even if the images still needs to be downloaded.

Here's 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 look into the examples, but didn't find out 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?


回答1:


In connection:didReceiveResponse: you must record how large the download is, for example in self.responseSize. Then, in connection:didReceiveData: you must append the data you just got to the data you previously got, 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; 
}


来源:https://stackoverflow.com/questions/12439536/mbprogresshud-and-sdwebimageprefetcher

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!