0
嗨,我正在實現'保存圖像到庫'功能,如下面的代碼片段所示。基本上,當用戶觸摸頁面上的圖像時會觸發照片保存。將圖像保存到庫時不會出現HUD微調器
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微調鎖定的過程。這是正確的嗎?如何解決微調顯示的滯後問題?
謝謝,這個作品!只是爲了澄清,這個performSelector究竟做了什麼?謝謝! – Zhen
您的應用程序執行「runloop」,基本上是頂級永不終止的while循環。在循環的每次迭代開始時,UI都會更新。當你調用一個方法來改變UI時,直到下一次循環纔會改變UI。所以當你執行長時間運行的東西時(比如你的保存操作),直到你的保存操作完成後,這個請求才會被執行,這會破壞HUD的目的:)我在http:// stackoverflow上詳細解釋了這一點。 COM /問題/ 7452925 /線程和-autoreleasepool-問題/ 7710576#7710576。 – edsko
哦,並且performSelector說:「在runloop的下一次迭代中調用此方法」 - 以便在顯示HUD之後執行。 – edsko