- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    [self dismissViewControllerAnimated:YES completion:^{}];

    [self.navigationController.tabBarController.view addSubview:self.waitingView];

    dispatch_queue_t currentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(currentQueue, ^{

        self.originalImgFromPicker = [info objectForKey:UIImagePickerControllerOriginalImage];

        CGSize size = self.originalImgFromPicker.size;
        if (size.width > 320)
        {
            size.height /= (size.width / 320);
            size.width = 320;
        }

        UIGraphicsBeginImageContext(size);
        [self.originalImgFromPicker drawInRect:CGRectMake(0,0,size.width,size.height)];
        self.resizedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        self.originalImgFromPicker = nil;

        NSData *resizedImageBinary = UIImageJPEGRepresentation(self.resizedImage, 1.0);

        self.resizedImageBinaryString = [NSString base64StringFromData:resizedImageBinary length:resizedImageBinary.length];

        dispatch_sync(currentQueue, ^{
            [self.waitingView removeFromSuperview];
        });
    });
}


我正在处理图像,在此期间,我使用带有活动指示器的透明视图来阻止UI。

但是此self.waitingView消失需要很长时间,然后应该消失。

当图像处理正在进行时,UI实际上会被self.waitingView阻止,这意味着当我尝试在没有任何作用的情况下轻按控件,但是当异步块中的图像处理完成后,self.waitingView仍然保留在屏幕上,并且当我尝试轻按任何控件时它回应。

如果我什么都不做,那将需要一些时间才能消失。

我想以一种活泼的方式做到这一点,我在代码中做错了什么?

最佳答案

UI操作应在主队列中完成

更改密码

         dispatch_sync(currentQueue, ^{
            [self.waitingView removeFromSuperview];
        });




        dispatch_sync(dispatch_get_main_queue(), ^{
            [self.waitingView removeFromSuperview];
        });


所以你的方法应该看起来像

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    [self dismissViewControllerAnimated:YES completion:^{}];

    [self.navigationController.tabBarController.view addSubview:self.waitingView];

    dispatch_queue_t currentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(currentQueue, ^{

        self.originalImgFromPicker = [info objectForKey:UIImagePickerControllerOriginalImage];

        CGSize size = self.originalImgFromPicker.size;
        if (size.width > 320)
        {
            size.height /= (size.width / 320);
            size.width = 320;
        }

        UIGraphicsBeginImageContext(size);
        [self.originalImgFromPicker drawInRect:CGRectMake(0,0,size.width,size.height)];
        self.resizedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        self.originalImgFromPicker = nil;

        NSData *resizedImageBinary = UIImageJPEGRepresentation(self.resizedImage, 1.0);

        self.resizedImageBinaryString = [NSString base64StringFromData:resizedImageBinary length:resizedImageBinary.length];

        dispatch_sync(dispatch_get_main_queue(), ^{
            [self.waitingView removeFromSuperview];
        });
    });
}

10-08 09:31