13

我一直在我的 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];
4

8 回答 8

19

您可以在动画块之前将 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];
于 2012-08-08T17:22:31.663 回答
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
        }
        }
    }
}
于 2016-04-01T21:28:28.887 回答
10

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

于 2018-07-31T17:11:38.453 回答
7

这是可以帮助我并工作得很好的代码。

photoImageView.sd_imageTransition = .fade
photoImageView.sd_setImage(with: URL(string: imageUrl), completed: nil)
于 2018-09-12T15:51:49.313 回答
2

这是@Zoltan Varadi的Swift 4版本的答案:

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
        }
    }
}
于 2018-05-16T11:13:15.193 回答
1

迅速:

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)

        }
    }
于 2015-02-19T08:11:00.317 回答
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;
    }
}];
于 2017-02-17T09:56:49.327 回答
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()
  }
}
于 2016-09-29T06:13:31.037 回答