[英]setBackgroundimage in UIButton using SDWebimage
我想使用SDWebImage設置UIButton背景圖像。
代碼: -
[btn.imageView setImageWithURL:[NSURL URLWithString:@"image url here"]
placeholderImage:[UIImage imageNamed:@"placeholder"]];
謝謝
在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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.