iOS SDWebImage fade in new image

后端 未结 8 1398
北海茫月
北海茫月 2021-01-31 06:36

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.

8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-31 07:02

    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
            }
            }
        }
    }
    

提交回复
热议问题