将图像从SDWebImage设置为按钮背景

时间:2016-03-29 06:59:27

标签: ios objective-c xcode sdwebimage

我在项目中使用SDWebImage,但我想知道是否有办法将图像设置为按钮的背景?

5 个答案:

答案 0 :(得分:3)

您只需要导入此类SDWebImage

#import "SDWebImage/UIButton+webCache.h"

UIButton+webCache此类是Category类,因此您可以像这样设置按钮背景图像,

[yourBTN sd_setBackgroundImageWithURL:[NSURL URLWithString:@"urlString.png"] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

答案 1 :(得分:3)

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];


                     }
                 }];

答案 2 :(得分:1)

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;

答案 3 :(得分:1)

Swift 3

被困了好几个小时。 Mayank Patel 的回答是正确的。

button.sd_setBackgroundImage(with: URL(string:url), for: .normal)

答案 4 :(得分:0)

以下内容对我有用

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

}