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.
This extension code worked better for me.
extension UIImageView {
public func setImageWithFadeFromURL(url: NSURL, placeholderImage placeholder: UIImage? = nil, animationDuration: Double = 0.3) {
self.sd_setImageWithURL(url, placeholderImage: placeholder) { (fetchedImage, error, cacheType, url) in
if error != nil {
print("Error loading Image from URL: \(url)\n(error?.localizedDescription)")
}
self.alpha = 0
self.image = fetchedImage
UIView.transitionWithView(self, duration: (cacheType == .None ? animationDuration : 0), options: .TransitionCrossDissolve, animations: { () -> Void in
self.alpha = 1
}, completion: nil)
}
}
public func cancelImageLoad() {
self.sd_cancelCurrentImageLoad()
}
}