4

我试图在使用方法MBProgressHUD下载 URL 列表时显示自定义。SDWebImagePrefetcherNSURLConnection

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

现在,我想展示NSLog自定义的进度,MBProgressHUD我希望 HUD 一直显示在屏幕上,直到流程完成,但我不知道该怎么做,另外,当我的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];
}

我试图研究这些例子,但没有找到如何做我想做的事。

编辑

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

        }
    }

有任何想法吗?

4

1 回答 1

4

connection:didReceiveResponse:您必须记录下载的大小,例如在 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; 
}
于 2012-09-17T17:37:22.690 回答