Created
September 29, 2015 18:14
-
-
Save ivanbruel/ec28ed9be5c1c69096e5 to your computer and use it in GitHub Desktop.
Animate crossfade UIColor and UIImage with SDWebImage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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