iOS SDWebImage淡入新图像

我一直在我的iPhone应用程序上使用SDWebImage来处理所有的图像加载。 我正在使用一个占位符图像,并且在加载后我想淡入或淡入新图像。 我正在使用成功块来设置图像,它工作的很好。 不pipe我尝试什么,图像都不会褪色。 我已经尝试将animation代码发送回主线程,但是这也没有帮助。 它只是立即加载…没有animation。

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

// 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]; 

您可以在animation块之前将imageView.alpha设置为0,然后在animation块中使其animation回到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]; 

对于SWIFT,我创build了这个扩展。 它只是淡入,当图像实际上必须从网上下载。 如果它来自caching,那么它不会褪色。

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

迅速:

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

这个扩展代码对我更好。

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

试试这个:

 [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; } }];