gpt4 book ai didi

objective-c - MBProgressHud 和 SDWebImagePrefetcher

转载 作者:可可西里 更新时间:2023-11-01 05:41:56 30 4
gpt4 key购买 nike

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

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

}
}

有什么想法吗?

最佳答案

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

关于objective-c - MBProgressHud 和 SDWebImagePrefetcher,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12439536/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com