我想使用SDWebImage设置UIButton背景图片。
代码: -
[btn.imageView setImageWithURL:[NSURL URLWithString:@"image url here"]
placeholderImage:[UIImage imageNamed:@"placeholder"]];
由于
答案 0 :(得分:32)
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;
答案 1 :(得分:3)
您可以使用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) {
}];
答案 2 :(得分: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];
答案 3 :(得分: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];
}];
答案 4 :(得分:0)
如果#import <SDWebImage/UIButton+WebCache.h>
是没有找到然后我认为你必须使用
#import“UIButton + WebCache.h”而不是
#import <SDWebImage/UIButton+WebCache.h>
这对我来说很好用
答案 5 :(得分: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
}