Q:

Need assistance regarding dispatch_sync and dispatch_async

- (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];
        });
    });
}

I am processing the image and during this I am blocking the UI with transparent view with activity indicator.
but this self.waitingView disappearing take long time then it should.
When the image processing is in progress the UI actually get block by self.waitingView means when I try to tap controls behind nothing work but when image process in async block get done self.waitingView still remain on screen and when I try to tap any control it responds.
If I do nothing it take some time to disappear.
I want to do this in snappy way, what I am doing wrong in code?

A:

UI operations should be done in the main queue
change the code
         dispatch_sync(currentQueue, ^{
            [self.waitingView removeFromSuperview];
        });

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

so u r method should look like
    - (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];
        });
    });
}

