2013-10-31 61 views
13

我想使用SDWebImage設置UIButton背景圖像。使用SDWebimage在UIButton中設置背景圖像

代碼: -

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

感謝

+0

我不是真的在這之後,很抱歉。你嘗試了什麼,當你嘗試時發生了什麼? – Robert

+0

@羅伯特嗨在uiimageview中設置相同的圖像,但在uibutton中沒有設置我使用這個方法https://github.com/rs/SDWebImage/blob/master/SDWebImage/UIButton%2BWebCache.h – Rushabh

+0

@Robert並使用這個鏈接https://github.com/rs/SDWebImage/issues/117 – Rushabh

回答

30

有此方法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; 
+0

@感謝它的作品:) :) :) – Rushabh

+0

不贊成使用的方法 – Aleksandr

+0

@亞歷山大謝謝引起我的注意。你可以編輯答案我會批准它。 – CRDave

1
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];  
      }]; 
+0

M.thanks回答,但這種方式不行,我已經試過這一個 – Rushabh

+0

編輯我的代碼。試一試。 –

2

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

可以使用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) { 

    }]; 
0

直接設置圖像的UIButton如果#import <SDWebImage/UIButton+WebCache.h> 沒有找到,那麼我認爲你必須使用 #進口「的UIButton + WebCache.h」,而不是 #import <SDWebImage/UIButton+WebCache.h>

這將很好地工作,我