Skip to content

Instantly share code, notes, and snippets.

@ivanbruel
Created September 29, 2015 18:14
Show Gist options
  • Save ivanbruel/ec28ed9be5c1c69096e5 to your computer and use it in GitHub Desktop.
Save ivanbruel/ec28ed9be5c1c69096e5 to your computer and use it in GitHub Desktop.
Animate crossfade UIColor and UIImage with SDWebImage
//
// UIImage+UIColor.swift
//
import UIKit
extension UIImage {
class func imageWithColor(color: UIColor) -> UIImage {
let rect = CGRectMake(0.0, 0.0, 1.0, 1.0)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
//
// UIImageView+SDWebImageColor.swift
//
//
import UIKit
import SDWebImage
extension UIImageView {
func sd_setImageWithURL(url: NSURL!, placeholderColor: UIColor?) {
if let placeholderColor = placeholderColor {
sd_setImageWithURL(url, placeholderImage: UIImage.imageWithColor(placeholderColor), options: .AvoidAutoSetImage) { (image, error, cache, url) -> Void in
if cache == SDImageCacheType.None {
UIView.transitionWithView(self, duration: 0.35, options: [.TransitionCrossDissolve, .CurveEaseOut], animations: { () -> Void in
self.image = image
}, completion: nil)
} else {
self.image = image
}
}
} else {
sd_setImageWithURL(url)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment