【问题标题】:setBackgroundimage in UIButton using SDWebimage使用 SDWebimage 在 UIButton 中设置背景图像
【发布时间】:2019-06-16 13:33:54
【问题描述】:

我想使用 SDWebImage 设置 UIButton 背景图片。

代码:-

[btn.imageView setImageWithURL:[NSURL URLWithString:@"image url here"]
                           placeholderImage:[UIImage imageNamed:@"placeholder"]];

谢谢

【问题讨论】:

标签: ios sdwebimage


【解决方案1】:

SDWebImage 中有这样的方法: SDWebImage / SDWebImage / UIButton+WebCache.h

在你的班级中导入这个文件:

#import <SDWebImage/UIButton+WebCache.h>

使用以下任何一种方法:

- (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state;
- (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder;
- (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options;
- (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state completed:(SDWebImageCompletionBlock)completedBlock;
- (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock;
- (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock;

【讨论】:

  • @Alexander 感谢您引起我的注意。你能编辑答案吗?我会批准的。
【解决方案2】:

您可以使用 SDWebImage 直接将图像设置为 UIButton

#import <SDWebImage/UIButton+WebCache.h>

 [btnProfilePic sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"IMAGE_URL_HERE"]]] forState:UIControlStateNormal];

或使用块

[btnProfilePic sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"IMAGE_URL_HERE"]]] forState:UIControlStateNormal placeholderImage:UIImage imageNamed:@"placeholderImage.png"] completed:
    ^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

    }];

【讨论】:

    【解决方案3】:

    我完全同意@CRDave,但是如果你必须使用 UIImageView 方法来处理 setImageWithPreviousCachedImageWithURL 之类的东西(在写作时它不是 UIButton 类别的一部分),那么不要在完成时设置按钮背景阻止,否则您的占位符或渐进式加载将不起作用。而是这样设置:

        UIImageView *temp = [[UIImageView alloc] init];
        [temp sd_setImageWithPreviousCachedImageWithURL:[NSURL URLWithString:stringUrl] andPlaceholderImage:[UIImage imageNamed:@"loading.png"] options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            //Nothing.
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            //Nothing.
        }];
        [btnTheButton setBackgroundImage:temp.image forState:UIControlStateNormal];
    

    【讨论】:

    • 好像这个方法已经不存在了。 setImageWithPreviousCachedImageWithURL
    【解决方案4】:
    UIImageView *imageViewb;
    [imageViewb setImageWithURL:[NSURL URLWithString:@"image url here"]
                                   placeholderImage:[UIImage imageNamed:@"placeholder"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
                    [button setImage: imageViewb.image forState:UIControlStateNormal];       
                }];
    

    【讨论】:

    • M.thanks for answer但这种方式行不通我已经尝试过这个
    【解决方案5】:

    如果#import &lt;SDWebImage/UIButton+WebCache.h&gt; 没有找到那么我认为你必须使用 #import "UIButton+WebCache.h" 而不是 #import &lt;SDWebImage/UIButton+WebCache.h&gt;

    这对我来说很好用

    【讨论】:

      【解决方案6】:

      我已经在这里回答了这个问题,但为了方便起见,我重新发布了答案:https://stackoverflow.com/a/56616298/3904109

      cell.bProfileImage.sd_setBackgroundImage(with: URL(string:
      individualChatsListArray[indexPath.row].profile_image), for:
      UIControl.State.normal, placeholderImage: UIImage(named:
      "default_profile"), options: SDWebImageOptions(rawValue: 0)) { (image,
      error, cache, url) in
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-14
        相关资源
        最近更新 更多