Pertanyaan setBackgroundimage di UIButton menggunakan SDWebimage


Saya ingin mengatur gambar latar belakang UIButton menggunakan SDWebImage.

Kode: -

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

Terima kasih


13
2017-10-31 10:43


asal


Jawaban:


Ada metode untuk ini di SDWebImage:  SDWebImage / SDWebImage / UIButton + WebCache.h

Impor file ini di kelas Anda:

#import <SDWebImage/UIButton+WebCache.h>

Gunakan salah satu metode ini:

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

30
2017-10-31 12:40



Anda dapat langsung mengatur gambar ke UIButton menggunakan SDWebImage

#import <SDWebImage/UIButton+WebCache.h>

 [btnProfilePic sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"IMAGE_URL_HERE"]]] forState:UIControlStateNormal];

Atau menggunakan blok

[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) {

    }];

3
2017-10-20 11:15



Saya sepenuhnya setuju dengan @CRDave, tetapi jika Anda harus menggunakan metode UIImageView untuk sesuatu seperti setImageWithPreviousCachedImageWithURL (yang saat ini bukan bagian dari kategori UIButton) maka jangan mengatur latar belakang tombol di blok penyelesaian jika tidak Anda placeholder atau pemuatan progresif tidak akan berfungsi. Alih-alih mengaturnya seperti ini:

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

2
2017-08-19 13:36



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

1
2017-10-31 10:57



jika #import <SDWebImage/UIButton+WebCache.h>  tidak ditemukan maka saya pikir Anda harus menggunakan #import "UIButton + WebCache.h" dari pada #import <SDWebImage/UIButton+WebCache.h>

Ini akan bekerja dengan baik untukku


0
2017-11-29 07:06