Search code examples
iphoneiosuser-interfaceprogress-bardelay

Delay between UIActivityIndicatorView animation stop and content view refresh


I have created a simple UIActivityIndicatorView that takes care of informing the user about the end of the execution for a specific task. My implementation is the following:

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
spinner.center = self.imageView.center;
[self.imageView addSubview:spinner];
[spinner startAnimating];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
    UIImage *filteredImage = ...some filtering...;
    self.imageView.image = filteredImage;
    [self.imageView setNeedsDisplay];

    dispatch_async(dispatch_get_main_queue(), ^{
        [spinner stopAnimating];
    });

});

However, when I run the application, the spinning wheel disappears and after a while the image is updated. Do you have any hint about how there is this delay?

EDIT: the setNeedsDisplay and the stopAnimating instructions are called in the right order. However, the UIImageView takes a while in order to update its content.

Thank you in advance.


Solution

  • The problem with your code is- You are trying to assigning image on imageview on other thread instead of main thread (GUI thread) so it takes time.

    To resolve this I have made few changes in your code-

    Instead of using this-

        UIImage *filteredImage = ...some filtering...;
        self.imageView.image = filteredImage;
        [self.imageView setNeedsDisplay];
    

    Try this-

    self.imageView.image = filteredImage;
    dispatch_async(dispatch_get_main_queue(), ^{
    
            self.imageView.image = filteredImage;
            [self.imageView setNeedsDisplay];
        });
    

    I have also faced this, hope it helps you.