How to crossfade between 2 images on iPhone using Core Animation

You were almost there.

CABasicAnimation *crossFade = [CABasicAnimation animationWithKeyPath:@"contents"];
crossFade.duration = 5.0;
crossFade.fromValue = image1.CGImage;
crossFade.toValue = image2.CGImage;
[self.imageView.layer addAnimation:crossFade forKey:@"animateContents"];

Note that the animation is independent of the actual values/contents of the UIImageView. Therefore you’ll need to

self.imageView.image = image2;

… to set the final result for your image.

Leave a Comment