我想使用SDWebImage設置UIButton背景圖像。使用SDWebimage在UIButton中設置背景圖像
代碼: -
[btn.imageView setImageWithURL:[NSURL URLWithString:@"image url here"]
placeholderImage:[UIImage imageNamed:@"placeholder"]];
感謝
我想使用SDWebImage設置UIButton背景圖像。使用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;
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];
}];
M.thanks回答,但這種方式不行,我已經試過這一個 – Rushabh
編輯我的代碼。試一試。 –
我完全同意@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
#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) {
}];
直接設置圖像的UIButton如果#import <SDWebImage/UIButton+WebCache.h>
沒有找到,那麼我認爲你必須使用 #進口「的UIButton + WebCache.h」,而不是 #import <SDWebImage/UIButton+WebCache.h>
這將很好地工作,我
我不是真的在這之後,很抱歉。你嘗試了什麼,當你嘗試時發生了什麼? – Robert
@羅伯特嗨在uiimageview中設置相同的圖像,但在uibutton中沒有設置我使用這個方法https://github.com/rs/SDWebImage/blob/master/SDWebImage/UIButton%2BWebCache.h – Rushabh
@Robert並使用這個鏈接https://github.com/rs/SDWebImage/issues/117 – Rushabh