iOS SDWebImage fade in new image

后端 未结 8 1401
北海茫月
北海茫月 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条回答
  •  故里飘歌
    2021-01-31 06:51

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

提交回复
热议问题