I want to set the UIButton background image using SDWebImage.
Code :-
[btn.imageView setImageWithURL:[NSURL URLWithString:@"image url here"]
placeholderImage:[UIImage imageNamed:@"placeholder"]];
Thanks
I want to set the UIButton background image using SDWebImage.
Code :-
[btn.imageView setImageWithURL:[NSURL URLWithString:@"image url here"]
placeholderImage:[UIImage imageNamed:@"placeholder"]];
Thanks
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;
您可以使用 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) {
}];
我完全同意@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];
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];
}];
如果#import <SDWebImage/UIButton+WebCache.h>
没有找到,那么我认为你必须使用
#import "UIButton+WebCache.h"而不是
#import <SDWebImage/UIButton+WebCache.h>
这对我来说很好用
我已经在这里回答了这个问题,但为了方便起见,我重新发布了答案: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
}