iOS SDWebImage淡入新图像

not*_*bob 11 asynchronous image fade ios sdwebimage

我一直在我的iPhone应用程序上使用SDWebImage来处理所有图像加载.我正在使用占位符图像,并且我想在加载后对新图像进行交叉淡入淡出或淡入淡出.我使用成功块来设置图像,它工作得很好.无论我尝试什么,图像都不会淡入.我已经尝试将动画代码发送回主线程,但这也无济于事.它只是立即加载......没有动画.

这是我的代码.有什么想法吗?

// load placeholder image
NSURL *url = ...
_imageView = [[UIImageView alloc] init];
[_imageView setImage:[UIImage imageNamed:@"loading.jpg"]];

// request image
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadWithURL:url
                delegate:self
                 options:0
                 success:^(UIImage *image) {

                     [UIView transitionWithView:_imageView
                                       duration:3.0
                                        options:UIViewAnimationOptionTransitionCrossDissolve
                                     animations:^{
                                         [_imageView setImage:image];
                                     } completion:NULL];

}
failure:nil];
Run Code Online (Sandbox Code Playgroud)

nic*_*ljs 20

您可以在动画块之前将imageView.alpha设置为0,然后在动画块中将其设置为动画回imageView.alpha = 1.0;

// load placeholder image
NSURL *url = ...
_imageView = [[UIImageView alloc] init];
[_imageView setImage:[UIImage imageNamed:@"loading.jpg"]];

// request image
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadWithURL:url
            delegate:self
             options:0
             success:^(UIImage *image, BOOL cached) {

                  imageView.alpha = 0.0;
                 [UIView transitionWithView:_imageView
                                   duration:3.0
                                    options:UIViewAnimationOptionTransitionCrossDissolve
                                 animations:^{
                                     [_imageView setImage:image];
                                      imageView.alpha = 1.0;
                                 } completion:NULL];

}
failure:nil];
Run Code Online (Sandbox Code Playgroud)


Zol*_*adi 10

对于SWIFT,我创建了这个扩展.当图像实际上必须从网上下载时,它才会淡入.如果它是从缓存中提供的,那么它就不会褪色.

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
        }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


ray*_*ray 6

SDWebImage 从4.3.0版开始,现在提供了内置的淡入淡出过渡。

imageView.sd_imageTransition = SDWebImageTransition.fadeTransition;
imageView.sd_setImage(with: ...)
Run Code Online (Sandbox Code Playgroud)

请参阅此处的文档,您可以使用其API执行更复杂的转换。

https://github.com/rs/SDWebImage/wiki/Advanced-Usage#image-transition-430


Che*_*tan 5

这是可以帮助我工作的代码。

photoImageView.sd_imageTransition = .fade
photoImageView.sd_setImage(with: URL(string: imageUrl), completed: nil)
Run Code Online (Sandbox Code Playgroud)