您好我正在实现'将图像保存到库'功能,如下面的代码段所示。基本上,当用户触摸页面上的图像时会触发照片保存。
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微调器。这是正确的吗?如何解决微调器显示中的延迟?
答案 0 :(得分:2)
是的,这是正确的。替换呼叫
[self saveImageToLibrary];
通过
[self performSelector:@selector(saveImageToLibrary) withObject:nil afterDelay:0];
这样HUD就有机会在你保存之前展示自己。