将 SDWebImage 中的图像设置为按钮的背景
Set image from SDWebImage to background of button
我在我的项目中使用了 SDWebImage,但我想知道是否有办法将图像设置为按钮的背景?
您只需导入 SDWebImage
的 class
#import "SDWebImage/UIButton+webCache.h"
和UIButton+webCache
这个class是类别class所以你可以像这样设置你的按钮背景图片,
[yourBTN sd_setBackgroundImageWithURL:[NSURL URLWithString:@"urlString.png"] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
SDWebImage中有这个方法
导入#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:(SDWebImageCompletedBlock)completedBlock;
- (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletedBlock)completedBlock;
- (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock;
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadImageWithURL:imageURL
options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize) {
// progression tracking code
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
if (image) {
// do something with image
[button setBackgroundImage:image forState:UIControlStateNormal];
}
}];
Swift 3
卡了几个小时。 Mayank Patel 的 回答是正确的。
button.sd_setBackgroundImage(with: URL(string:url), for: .normal)
以下对我有用
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
}
我在我的项目中使用了 SDWebImage,但我想知道是否有办法将图像设置为按钮的背景?
您只需导入 SDWebImage
#import "SDWebImage/UIButton+webCache.h"
和UIButton+webCache
这个class是类别class所以你可以像这样设置你的按钮背景图片,
[yourBTN sd_setBackgroundImageWithURL:[NSURL URLWithString:@"urlString.png"] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
SDWebImage中有这个方法
导入#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:(SDWebImageCompletedBlock)completedBlock;
- (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletedBlock)completedBlock;
- (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock;
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadImageWithURL:imageURL
options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize) {
// progression tracking code
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
if (image) {
// do something with image
[button setBackgroundImage:image forState:UIControlStateNormal];
}
}];
Swift 3
卡了几个小时。 Mayank Patel 的 回答是正确的。
button.sd_setBackgroundImage(with: URL(string:url), for: .normal)
以下对我有用
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
}