MBProgressHud和SDWebImagePrefetcher [英] MBProgressHud and SDWebImagePrefetcher
问题描述
我正在尝试展示自定义 MBProgressHUD
使用 SDWebImagePrefetcher <下载URL列表时/ code>
使用 NSURLConnection
方法。
I'm trying to show a custom MBProgressHUD
while downloading a list of URLs with SDWebImagePrefetcher
using NSURLConnection
methods.
SDWebImagePrefetcher
有一个方法,在调用时,在控制台中显示图像下载的进度。
SDWebImagePrefetcher
has a method that, when called, shows in console the progress of the images download.
现在,我想显示 NSLog
自定义 MBProgressHUD
的进度,我希望HUD保持在屏幕上,直到完成该过程,但我不知道如何做到这一点加上,当我的 NSURLConnection
方法被调用时,它会显示初始HUD(连接),然后快速跳转到完成,即使图像仍然需要下载。
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.
这是我的代码:
- (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.
编辑
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;
}
}
}
任何想法?
推荐答案
在连接:didReceiveResponse:
你必须记录多大的下载是,例如self.responseSize中的
。然后,在 connection:didReceiveData:
你
必须将你刚刚获得的数据附加到你以前获得的数据上,并更新进度:
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;
}
这篇关于MBProgressHud和SDWebImagePrefetcher的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!