您好,我正在实现“将图像保存到库”功能,如下面的代码片段所示。基本上,当用户触摸页面上的图像时会触发照片保存。
TouchImageView *tiv = [[TouchImageView alloc]initWithFrame:blockFrame];
[tiv setCompletionHandler:^(NSString *imageUrl){
//Spinner should start after user clicks on an image
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
hud.labelText = @"Saving photo to library";
//trigger method to save image to library
[self saveImageToLibrary];
}];
-(void) saveImageToLibrary
{
//convert url to uiimage
UIImage *selectedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]]];
//Save image to album
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
// Request to save the image to camera roll
[library writeImageToSavedPhotosAlbum:[selectedImage CGImage] orientation:(ALAssetOrientation)[selectedImage imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
[MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
if (error) {
NSLog(@"error");
} else {
NSLog(@"url %@", assetURL);
//Trigger get photo from library function
self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:self.imgPicker animated:YES];
}
}];
[library release];
}
问题是 HUD 微调器没有出现(在延迟 3-4 秒后),我怀疑“writeImageToSavedPhotosAlbum:”是同步的并锁定了显示 HUD 微调器的进程。这是正确的吗?如何解决微调器显示延迟的问题?
最佳答案
是的,没错。替换调用
[self saveImageToLibrary];
通过
[self performSelector:@selector(saveImageToLibrary) withObject:nil afterDelay:0];
这样 HUD 就有机会在您保存之前显示自己。
关于objective-c - 将图像保存到库时不出现 HUD 微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7847917/