如何在保存图像库IOS7时添加MBProgressHUD
您好,在我的应用程序中,我的图像位于如何在保存图像库IOS7时添加MBProgressHUD,ios,image,download,mbprogresshud,Ios,Image,Download,Mbprogresshud,您好,在我的应用程序中,我的图像位于UIImageView上,我有一个名为下载的按钮,用户单击下载按钮后,图像将保存到Photolibrary。现在我想为下载添加Progressbar,以了解用户正在下载图像 我已经使用了MBProgressHUD来进行进度,但不能像我想要的那样工作。我想显示一次用户点击下载按钮,如其下载消息和进度一次图像下载我想显示图像下载请告诉我如何实现这一点我累了,类似的东西它不工作 我的MBProgressHUDcode - (IBAction)down:(id)sen
UIImageView
上,我有一个名为下载的按钮,用户单击下载按钮后,图像将保存到Photolibrary
。现在我想为下载添加Progressbar,以了解用户正在下载图像
我已经使用了MBProgressHUD
来进行进度,但不能像我想要的那样工作。我想显示一次用户点击下载按钮,如其下载消息和进度一次图像下载我想显示图像下载请告诉我如何实现这一点我累了,类似的东西它不工作
我的MBProgressHUD
code
- (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);
}
}
-(iAction)关闭:(id)发送方{
HUD=[[MBProgressHUD alloc]initWithView:self.view];
HUD.labelText=@“下载…”;
HUD.mode=MBProgressHUDMODE环形终止;
[self.view addSubview:HUD];
[HUD显示执行时:@selector(下载)onTarget:self with object:nil动画:YES];
UIImageWriteToSavedPhotosAlbum(imageview.image,nil,nil,nil);
}
-(作废)下载{
浮动进度=0.0;
而(进度<1.0){
进度+=0.01;
HUD.progress=进度;
美国LEEP(50000);
}
}
我已经用上面的代码来实现,但它不是我想要的工作,请告诉我如何实现这一个
谢谢。UIImageWriteToSavedPhotosAlbum在主线程上工作。所以,把这个函数放在GCD中。当主线程繁忙时,您的UI无法自我更新。请尝试以下操作:
- (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;
});
}
您可以使用显示进度条。如果您正在从服务器下载图像,请使用异步调用。例如,如果您使用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*)请求
{
n错误*错误=[请求错误]
if (!error)
{
NSString *response = [request responseString];
NSMutableDictionary *responseJSON = [response JSONValue];
[SVProgressHUD dismiss];
self.view.userInteractionEnabled = YES;
UIImageWriteToSavedPhotosAlbum(imageview.image, nil, nil, nil);
}
}
希望这对您有所帮助。-(iAction)down:(id)发件人{
- (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];
});
}
}
HUD=[MBProgressHUD ShowHUD AddedTo:self.view动画:是];
HUD.mode=MBProgressHUDMODE环形终止;
NSString*strloadingText=[NSString stringWithFormat:@“下载…”;
HUD.labelText=strloadingText;
[抬头显示器显示:是];
[HUD显示在执行时:@selector(doSomeFunkyStuff)onTarget:selfwithobject:nil动画:YES];
}
-(无效)doSomeFunkyStuff{
浮动进度=0.0;
而(进度<1.0){
进度+=0.01;
HUD.progress=进度;
美国LEEP(5000000);
调度异步(调度获取全局队列(调度队列优先级低,0),
^{
//后台线程中的进程
NSString*strloadingText=[NSString stringWithFormat:@“已完成”];
HUD.labelText=strloadingText;UIImageWriteToSavedPhotosAlbum(imageview.image,nil,nil);
[抬头显示器隐藏:是];
});
}
}
很抱歉,没有为ios开发提供最新的即时消息,因此请告诉我,我想更改此链接的原因:它在[HUD_代码中显示错误];像未使用的标识符hud_代码一样,请告诉我在哪里声明此代码hud_代码
只是我的示例。你应该使用你的代码关闭这个HUD。现在它不工作了,甚至没有显示进度条。请告诉我如何解决这个问题