嗨,我正在实现“将图像保存到库”功能,如下面的代码片段所示。基本上,当用户触摸页面上的图像时会触发照片保存。
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微调器的过程。这是正确的吗?如何解决微调器显示的滞后?