iOS SDWebImage淡入新图像

时间:2012-08-08 16:51:41

标签: ios image asynchronous fade 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];

8 个答案:

答案 0 :(得分: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];

答案 1 :(得分: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
        }
        }
    }
}

答案 2 :(得分:4)

SDWebImage现在提供了自4.3.0起的内置淡入淡出过渡。

imageView.sd_imageTransition = SDWebImageTransition.fadeTransition;
imageView.sd_setImage(with: ...)

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

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

答案 3 :(得分:3)

下面的代码可以帮助我,并使其正常工作。

photoImageView.sd_imageTransition = .fade
photoImageView.sd_setImage(with: URL(string: imageUrl), completed: nil)

答案 4 :(得分:2)

这是 Swift 4 版@Zoltan Varadi回答:

extension UIImageView {
    public func sd_setImageWithURLWithFade(url: URL!, placeholderImage placeholder: UIImage!) {
        self.sd_setImage(with: url, placeholderImage: placeholder) { (image, error, cacheType, url) -> Void in
            if let downLoadedImage = image {
                if cacheType == .none {
                    self.alpha = 0
                    UIView.transition(with: self, duration: 0.3, options: UIViewAnimationOptions.transitionCrossDissolve, animations: { () -> Void in
                        self.image = downLoadedImage
                        self.alpha = 1
                    }, completion: nil)   
                }
            } else {
                self.image = placeholder
            }
        }
    }
}

我将持续时间更改为0.3

您可以将此功能添加到扩展中,以便您需要completionHandler块:

public func sd_setImageWithURLWithFade(url: URL!, placeholderImage placeholder: UIImage!, comple: @escaping (Bool)->()) {
    self.sd_setImage(with: url, placeholderImage: placeholder, options: .allowInvalidSSLCertificates) { (image, error, cacheType, url) in
        if let downLoadedImage = image {
            if cacheType == .none {
                self.alpha = 0
                UIView.transition(with: self, duration: 0.3, options: UIViewAnimationOptions.transitionCrossDissolve, animations: { () -> Void in
                    self.image = downLoadedImage
                    self.alpha = 1
                }, completion: { _ in
                    comple(true)
                })
            }
        } else {
            self.image = placeholder
        }
    }
}

答案 5 :(得分:1)

<强> SWIFT:

func setSDWebImageWithAnimation(imageViewToSet mImageView:UIImageView, URLToSet imageURL:NSURL!)
    {
        mImageView.image = UIImage(named: "favouritePlaceholder")
        SDWebImageManager.sharedManager().downloadImageWithURL(imageURL, options: nil, progress: nil) { (downloadedImage:UIImage!, error:NSError!, cacheType:SDImageCacheType, isDownloaded:Bool, withURL:NSURL!) -> Void in
            mImageView.alpha = 0
            UIView.transitionWithView(mImageView, duration: 1.0, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: { () -> Void in
                mImageView.image = downloadedImage
                mImageView.alpha = 1
                }, completion: nil)

        }
    }

答案 6 :(得分:1)

试试这个:

[self.myImage sd_setImageWithURL:storyThumbnailURL placeholderImage:[UIImage imageNamed:@"xyz"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    if (cacheType == SDImageCacheTypeNone) {
        self.myImage.alpha = 0;
        [UIView animateWithDuration:0.3 animations:^{
            self.myImage.alpha = 1;
        }];
    } else {
        self.myImage.alpha = 1;
    }
}];

答案 7 :(得分:0)

此扩展程序代码对我来说效果更好。

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