简体   繁体   中英

Show UIButton image as round with SDWebImage

My code used to work before switching to SDWebImage. But now it is not working. I mean, the image is presented as a square as opposed to a circle. Here is my code

- (void)makeImageViewRound:(UIButton *)imageButton
{
    imageButton.imageView.layer.cornerRadius =  imageButton.imageView.bounds.size.width/2;
    imageButton.imageView.clipsToBounds = YES;
    imageButton.imageView.layer.borderWidth=0.5;
    imageButton.imageView.layer.masksToBounds = YES;
    imageButton.imageView.layer.borderColor=[[UIColor blackColor] CGColor];
}

…
self makeImageViewRound: self.catImageButton];
…
[self.catImageButton sd_setImageWithURL:catUrl forState:UIControlStateNormal];

I would rather not have to go in and change SDWebImage itself to solve this.

Did you tried to round your view in completion block of another method from SDWebImage's api?

- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletedBlock)completedBlock;


If that still won't work you can always round firstly your downloaded image. Check this question:

Rounded Corners on UIImage

Use completion block and GCD to change image view in main thread after applying the new image.

- (void)makeImageViewRound:(UIButton *)imageButton {
    imageButton.imageView.layer.cornerRadius =  imageButton.imageView.bounds.size.width/2;
    imageButton.imageView.clipsToBounds = YES;
    imageButton.imageView.layer.borderWidth=0.5;
    imageButton.imageView.layer.masksToBounds = YES;
    imageButton.imageView.layer.borderColor=[[UIColor blackColor] CGColor];
}

....

[catImageButton sd_setImageWithURL:catUrl forState:UIControlStateNormal completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    if (error) {
        return;
    }

    __weak typeof (self) weak_self = self;

    dispatch_async(dispatch_get_main_queue(), ^{
       [weak_self makeImageViewRound: weak_self.catImageButton];
    });
}];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM