I\'ve been using SDWebImage on my iPhone app to handle all of the image loading. I am using a placeholder image, and I want to crossfade or fade in the new image once it loads.
For SWIFT, i created this extension. It only fades in, when the image actually had to be downloaded from the web. If it was served from the cache, then it won't fade.
import UIKit
import SDWebImage
extension UIImageView {
public func sd_setImageWithURLWithFade(url: NSURL!, placeholderImage placeholder: UIImage!)
{ self.sd_setImageWithURL(url, placeholderImage: placeholder) { (image, error, cacheType, url) -> Void in
if let downLoadedImage = image
{
if cacheType == .None
{
self.alpha = 0
UIView.transitionWithView(self, duration: 0.2, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: { () -> Void in
self.image = downLoadedImage
self.alpha = 1
}, completion: nil)
}
}
else
{
self.image = placeholder
}
}
}
}