16

I want to set the UIButton background image using SDWebImage.

Code :-

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

Thanks

4

6 回答 6

36

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;
于 2013-10-31T12:40:45.860 回答
4

您可以使用 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) {

    }];
于 2015-10-20T11:15:37.190 回答
2

我完全同意@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];
于 2014-08-19T13:36:38.993 回答
1
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];       
            }];
于 2013-10-31T10:57:20.673 回答
0

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

这对我来说很好用

于 2016-11-29T07:06:55.077 回答
0

我已经在这里回答了这个问题,但为了方便起见,我重新发布了答案: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

}
于 2019-06-16T05:23:33.337 回答