如何在 SDWebImage 中显示活动指示器
Posted
技术标签:
【中文标题】如何在 SDWebImage 中显示活动指示器【英文标题】:How to show an activity indicator in SDWebImage 【发布时间】:2012-02-22 02:38:00 【问题描述】:目前我正在通过以下操作将 SDWebImage 集成到我的项目中
1)#import "UIButton+WebCache.h"
2)[按钮 setImageWithURL:url placeholderImage:[UIImage imageNamed:@"no_photo.png"]];
因此它将在相应按钮上方的 URL 中显示图像列表。
但是现在我想在下载图像时在按钮上方显示一个活动指示器,那么我该怎么做呢?
【问题讨论】:
我相信现在可以使用我提供的解决方案来实现这一点。请看一看。 【参考方案1】:对我来说就像一个魅力:
斯威夫特 3:
imgView.setShowActivityIndicator(true)
imgView.setIndicatorStyle(.gray)
imgView.sd_setImage(with: URL(string: urlString), placeholderImage: UIImage(named: "placeholder"))
Swift 4:(编辑:更新)
imgView.sd_setShowActivityIndicatorView(true)
imgView.sd_setIndicatorStyle(.gray)
imgView.sd_setImage(with: URL(string: urlString), placeholderImage: UIImage(named: "placeholder"))
Swift 5:SDWebImage (5.x.x)
imgView.sd_imageIndicator = SDWebImageActivityIndicator.gray
imgView.sd_setImage(with: URL(string: urlString), placeholderImage: UIImage(named: "placeholder"))
更新:对于 UIButton 使用这个
yourButton.sd_setImage(with: URL(string: urlString), for: .normal)
【讨论】:
这应该是选择的答案。真的是一种魅力 图片加载到imageview时是否需要调用stop animating或类似的方法? 不,只要获取图像,您的指示器就会自动隐藏。如果这解决了您的问题,请点赞。 在 Swift 4 中实际上是 imgView.sd_setShowActivityIndicatorView(true) imgView.sd_setIndicatorStyle(.gray) 不适用于按钮的 imageView。相反,您应该使用 button.sd_setImage(with: url, for: .normal),所以直接使用按钮。【参考方案2】:最后的解决方案
您可以下载UIActivityIndicator-for-SDWebImage,这是将 UIActivityView 添加到您的SDWebImage 视图的最简单方法。使用 CocoaPods,只需将此行添加到您的 podfile:
pod 'UIActivityIndicator-for-SDWebImage'
您可以根据自己的喜好使用以下行之一:
- (void)setImageWithURL:(NSURL *)url usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)activityStyle;
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)activityStyle;
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)activityStyle;
- (void)setImageWithURL:(NSURL *)url completed:(SDWebImageCompletionBlock)completedBlock usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)activityStyle;
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)activityStyle;
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)activityStyle;
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock usingActivityIndicatorStyle:(UIActivityIndicatorViewStyle)activityStyle;
使用示例
只需导入
#import <UIActivityIndicator-for-SDWebImage/UIImageView+UIActivityIndicatorForSDWebImage.h>
并使用此代码
[imageView setImageWithURL:[NSURL URLWithString:@"https://media.licdn.com/mpr/mpr/wc_200_200/p/1/005/07f/0a3/30cb8dd.jpg"] placeholderImage:[UIImage imageNamed:@"myImage.jpg"] usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
【讨论】:
【参考方案3】:我发现最好的方法是使用 SDWebImageManager 类。然后,您的视图控制器或其他一些类将需要符合 SDWebImageManagerDelegate 协议。
SDWebImageManager *manager = [SDWebImageManager sharedManager];
UIImage *cachedImage = [manager imageWithURL:url];
if (cachedImage)
[button setImage:cachedImage];
// stop or remove your UIActivityIndicatorView here
else
[manager downloadWithURL:url delegate:self];
图像下载完成后,将调用委托方法:
- (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image
[button setImage:image];
// stop or remove your UIActivityIndicatorView here
还有一个代理方法,用于当下载图像发生错误时
- (void)webImageManager:(SDWebImageManager *)imageManager didFailWithError:(NSError *)error
// Handle error here
如果您有多个按钮,则在图像下载后确定哪个图像属于哪个按钮时可能会遇到问题。在这种情况下,您可能需要一个按钮子类来处理上述下载,然后更新自己的图像。
希望对您有所帮助。
【讨论】:
【参考方案4】:最好的方法是在 UIImageView+WebCache.m 的所有“setImage”函数中添加activityIndicator,然后在“webImageManager:r didFinishWithImage:”中删除它,我在设备中测试它,它工作顺利,她是示例:
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder
UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleWhiteLarge)];
[activity startAnimating];
[activity setCenter:self.center];
[self addSubview:activity];
[activity release];
SDWebImageManager *manager = [SDWebImageManager sharedManager];
UIImage *cachedImage = [manager imageWithURL:url];
if (cachedImage)
// the image is cached -> remove activityIndicator from view
for (UIView *v in [self subviews])
if ([v isKindOfClass:[UIActivityIndicatorView class]])
[activity removeFromSuperview];
[self setImageWithURL:url placeholderImage:placeholder options:0];
然后你用淡入淡出动画将其移除;):
- (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image
for (UIView *v in [self subviews])
if ([v isKindOfClass:[UIActivityIndicatorView class]])
// NSLog(@"-didFinishWithImage-");
[((UIActivityIndicatorView*)v) stopAnimating];
[v removeFromSuperview];
[UIView beginAnimations:@"fadeAnimation" context:NULL];
[UIView setAnimationDuration:0.9];
self.alpha = 0;
self.image = image;
self.alpha=1;
//[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self cache:YES];
[UIView commitAnimations];
【讨论】:
我崩溃了,加载图像时出现断言失败错误。 我只是下载并使用 github 中的最后一个版本,它工作正常(ios 5.1)。似乎 imageWithURL: 已被弃用,我会尝试为此使用新版本,但您必须给我们一些代码(您使用的是 UIImageView 网关吗?) 每当我使用您的代码添加活动指示器时,它都会向我提供断言错误,但如果没有活动指示器,其他动画代码将像魅力一样工作。如果您尝试告诉我有关新版本的信息,那对我有好处。【参考方案5】:对于 Swift 5
//pass true for showing indicator View
self.imageView.sd_setShowActivityIndicatorView(true)
//give style value
self.imageView.sd_setIndicatorStyle(UIActivityIndicatorView.Style.gray)
//Set your image as you are want to do
self.imageView.sd_setImage(with: URL(string: self.imageUrl), placeholderImage: UIImage())
【讨论】:
【参考方案6】:对于 Swift 5 用户可以通过这种方式使用 SDWebImageManager
extension UIImageView
func getImage(url: String, placeholderImage: UIImage?, success:@escaping (_ _result : Any? ) -> Void, failer:@escaping (_ _result : Any? ) -> Void)
self.sd_imageIndicator = SDWebImageActivityIndicator.gray
self.sd_setImage(with: URL(string: url), placeholderImage: placeholderImage, options: SDWebImageOptions(rawValue: 0), completed: image, error, cacheType, imageURL in
// your rest code
if error == nil
self.image = image
success(true)
else
failer(false)
)
用法:-
var imageFeatureImage: String = ""
if !imageURL.contains("https://")
imageFeatureImage = BASE_URL_IMAGE+imageURL
yourimageView.getImage(url: (imageFeatureImage), placeholderImage: nil) (success) in
yourimageView.contentMode = .scaleAspectFill
failer: (failed) in
yourimageView.image = UIImage.init(named: "placeholder")
yourimageView.contentMode = .scaleAspectFit
【讨论】:
【参考方案7】:目标-C:SDWebImage (5.x.x)
[imgView setSd_imageIndicator:SDWebImageActivityIndicator.grayIndicator];
[imgView sd_setImageWithURL:[NSURL URLWithString: urlString] placeholderImage:nil];
【讨论】:
以上是关于如何在 SDWebImage 中显示活动指示器的主要内容,如果未能解决你的问题,请参考以下文章
如何在 SDWebImage 下载器中添加 UIImageView (UIActivityIndicatorForSDWebImage)?