Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 将图像保存到库时,HUD微调器不显示_Objective C_Ios_Photo Gallery_Mbprogresshud - Fatal编程技术网

Objective c 将图像保存到库时,HUD微调器不显示

Objective c 将图像保存到库时,HUD微调器不显示,objective-c,ios,photo-gallery,mbprogresshud,Objective C,Ios,Photo Gallery,Mbprogresshud,您好,我正在实现“将图像保存到库”功能,如下面的代码片段所示。基本上,当用户触摸页面上的图像时会触发照片保存 TouchImageView *tiv = [[TouchImageView alloc]initWithFrame:blockFrame]; [tiv setCompletionHandler:^(NSString *imageUrl){ //Spinner should start after user clicks on an image

您好,我正在实现“将图像保存到库”功能,如下面的代码片段所示。基本上,当用户触摸页面上的图像时会触发照片保存

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];


这样,在保存之前,HUD有机会显示自己。

谢谢,这很有效!只是想澄清一下,这个performSelector到底做了什么?谢谢你的应用程序执行一个“runloop”,基本上是一个顶级的while循环。在循环的每个迭代开始时,UI都会得到更新。当您调用一个方法来更改UI时,在循环的下一次迭代之前,UI实际上不会更改。因此,当您执行一些长时间运行的操作(如save操作)时,该请求在save操作完成之前不会执行,这与HUD的目的背道而驰:)我在.Oh中对此进行了更详细的解释,performSelector说:“在runloop的下一次迭代中调用此方法”--显示HUD后将执行此操作。谢谢!这让我明白了:)谢谢你的帮助
[self performSelector:@selector(saveImageToLibrary) withObject:nil afterDelay:0];