-1

I'm trying to do a transition animation from UIImage background color to an actual image loaded from server. I found that the transition make the UITableView stutter while the animation is happening. Is there a way to make the transition smooth without causing the stutter.

float fadeDuration = 1.0f;
imageView.backgroundColor = [UIColor averageColor];
UIImage * toImage = [UIImage imageWithData:data];
                [UIView transitionWithView:imageView
                                  duration:fadeDuration
                                   options:UIViewAnimationOptionTransitionCrossDissolve
                                animations:^{
                                    imageView.image = toImage;
                                } completion:nil];
Geeroz
  • 666
  • 7
  • 9

1 Answers1

0

Since you first call getDataInBackgroundWithBlock the code you write in that block is not executed on the main thread but one some other thread. But UI changes have to be performed on the UI / main thread.

To get the code to be exectued on the main thread you have to wrap the UIView transitionWithView call into an dispatch_asnyc:

// ... some non-UI code
UIImage * toImage = [UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
    [UIView transitionWithView:imageView
                      duration:fadeDuration
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{
                        imageView.image = toImage;
                    } completion:nil
    ];
    // ... more UI code
});
// ... more non-UI code
luk2302
  • 55,258
  • 23
  • 97
  • 137