0
- (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消失需要很长时间才应该。

当图像处理正在进行时,self.waitingView当我尝试点击没有任何工作的控件但当异步块中的图像处理完成self.waitingView时仍然保留在屏幕上并且当我尝试点击任何控件时它会响应时,UI实际上会被阻塞。

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

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

4

2 回答 2

2

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];
        });
    });
}
于 2013-06-18T05:54:15.963 回答
0

代替

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

dispatch_async(dispatch_get_main_queue(), ^{
        [self.waitingView removeFromSuperview];
    }); 
于 2013-06-18T05:58:08.807 回答