Ios 使用SDWebimage在UIButton中设置背景图像

Ios 使用SDWebimage在UIButton中设置背景图像,ios,sdwebimage,Ios,Sdwebimage,我想使用SDWebImage设置UIButton背景图像 代码:- [btn.imageView setImageWithURL:[NSURL URLWithString:@"image url here"] placeholderImage:[UIImage imageNamed:@"placeholder"]]; 谢谢在SDWebImage中有这样的方法: 在类中导入此文件: #import <SDWebImage/UIButto

我想使用SDWebImage设置UIButton背景图像

代码:-

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

谢谢

在SDWebImage中有这样的方法:

在类中导入此文件:

#import <SDWebImage/UIButton+WebCache.h>

我完全同意@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];

您可以使用SDWebImage直接将图像设置为UIButton

#import <SDWebImage/UIButton+WebCache.h>

 [btnProfilePic sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"IMAGE_URL_HERE"]]] forState:UIControlStateNormal];
如果
#导入

没有找到的话我想你要用
#导入“UIButton+WebCache.h”而不是
#导入


这对我来说很好

我在这里回答了这个问题,但为了方便起见,我重新发布了答案:


对不起,我不太明白。您尝试过什么?尝试时会发生什么?@Robert hi在uiimageview中设置了相同的图像,但在uibutton中未设置它我正在使用此方法@Robert并使用此链接M。感谢您的回答,但这种方法不起作用我已经尝试过了one@Alexander谢谢你引起我的注意。你能编辑答案吗?我会批准的。看起来这个方法已经不存在了
setImageWithPreviousCachedImageWithURL
    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];
#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) {

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

}