嗨,在我的应用程序中,我有UIImageView
上的图像,我有一个名为下载的按钮,一旦用户点击下载按钮,图像将保存到Photolibrary
。现在我想添加下载进度条以了解用户下载图像。
我已经使用MBProgressHUD
来取得进展但却无法像我想要的那样工作。我想显示一旦用户点击下载按钮,如下载消息,一旦图像下载我想要显示图像下载,请告诉我如何实现这一个我厌倦了这样的事情。
我的MBProgressHUD
代码。
- (IBAction)down:(id)sender {
HUD = [[MBProgressHUD alloc] initWithView:self.view];
HUD.labelText = @"downloading...";
HUD.mode = MBProgressHUDModeAnnularDeterminate;
[self.view addSubview:HUD];
[HUD showWhileExecuting:@selector(download) onTarget:self withObject:nil animated:YES];
UIImageWriteToSavedPhotosAlbum(imageview.image, nil, nil, nil);
}
- (void)download {
float progress = 0.0;
while (progress < 1.0) {
progress += 0.01;
HUD.progress = progress;
usleep(50000);
}
}
我已经使用上面的代码来实现,但它不能像我想的那样工作,请告诉我如何实现这个。
感谢。
答案 0 :(得分:1)
- (IBAction)down:(id)sender {
HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
HUD.mode =MBProgressHUDModeAnnularDeterminate;
NSString *strloadingText = [NSString stringWithFormat:@"Downloading...."];
HUD.labelText = strloadingText;
[HUD show:YES];
[HUD showWhileExecuting:@selector(doSomeFunkyStuff) onTarget:self withObject:nil animated:YES];
}
- (void)doSomeFunkyStuff {
float progress = 0.0;
while (progress < 1.0) {
progress += 0.01;
HUD.progress = progress;
usleep(5000000);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0),
^{
// process in background thread
NSString *strloadingText = [NSString stringWithFormat:@"completed"];
HUD.labelText = strloadingText;UIImageWriteToSavedPhotosAlbum(imageview.image, nil, nil, nil);
[HUD hide:YES];
});
}
}
答案 1 :(得分:0)
UIImageWriteToSavedPhotosAlbum适用于主线程。所以,把这个功能放在GCD中。主线程忙时,您的UI无法自行更新。
答案 2 :(得分:0)
试试这个:
- (IBAction)down:(id)sender {
...
[HUD showWhileExecuting:@selector(download) onTarget:self withObject:nil animated:YES];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0),
^{
// process in background thread
UIImageWriteToSavedPhotosAlbum(imageview.image, nil, nil, nil);
// when finish, dismiss HUD
HUD.hidden = YES;
});
}
答案 3 :(得分:0)
您可以使用https://github.com/samvermette/SVProgressHUD显示进度条。如果您从服务器下载图像,请使用异步调用。就像您使用ASIFormDataRequest请求获取图像一样,然后使用如下:
[SVProgressHUD showWithStatus:@"Please wait"];
self.view.userInteractionEnabled = NO;
NSURL *url=[NSURL URLWithString:@"your URL request"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:@"POST"];
[request setPostValue:xyz forKey:@"abc"];
[request.shouldAttemptPersistentConnection = NO;
request.delegate=self;
request.timeOutSeconds = 10;
[request startAsynchronous];
在此之后你可以打电话:
- (void)requestFinished:(ASIHTTPRequest *)请求 { NSError * error = [请求错误];
if (!error)
{
NSString *response = [request responseString];
NSMutableDictionary *responseJSON = [response JSONValue];
[SVProgressHUD dismiss];
self.view.userInteractionEnabled = YES;
UIImageWriteToSavedPhotosAlbum(imageview.image, nil, nil, nil);
} }
希望这会对你有所帮助。