【问题标题】:MBProgressHud and SDWebImagePrefetcherMBProgressHud 和 SDWebImagePrefetcher
【发布时间】:2012-09-15 17:24:03
【问题描述】:

我正在尝试使用NSURLConnection 方法下载带有SDWebImagePrefetcher 的URL 列表时显示自定义MBProgressHUD

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

现在,我想在自定义MBProgressHUD 中显示NSLog 的进度,并且我希望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;
            }

        }
    }

有什么想法吗?

【问题讨论】:

  • 您没有展示如何设置 HUD。你能证明吗?
  • 谢谢。事实证明这不是问题 - 我错过了您实际上没有在任何地方更新进度! :-)

标签: objective-c ios nsurlconnection mbprogresshud sdwebimage


【解决方案1】:

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

【讨论】:

  • 谢谢,我试过那样做,但问题是HUD不会在屏幕上持续显示。我认为这可能取决于请求的 url,我应该请求所有图像 url 还是?
  • 如果您请求许多图像并且只有一个 HUD,那么您最终可能会一遍又一遍地隐藏/显示 HUD,而没有时间实际显示任何进度更新,是的。您需要构建一个抽象来处理它。
  • 实际上我正在获取图像并将其存储在缓存中以供以后使用,但是该进程在后台运行并且可能会减慢应用程序的速度,因此我想在整个进程时间内显示一个 HUD,然后完成后隐藏
  • 那么您需要为整个批次设置一次HUD,并且可能为每个下载的图像更新一次进度。因此,对于 TOTAL 的图像 N,您更新 HUD.progress = (float)N / TOTAL;。如何在更高层次上应用它留给读者作为练习。 (或者也许是一个不同问题的主题?)
猜你喜欢
  • 2014-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-28
  • 2012-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多