简体   繁体   English

使用SDWebImage将UIButton图像显示为圆形

[英]Show UIButton image as round with SDWebImage

My code used to work before switching to SDWebImage. 我的代码曾经在切换到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. 我宁愿不必进入并更改SDWebImage本身来解决此问题。

Did you tried to round your view in completion block of another method from SDWebImage's api? 您是否尝试过从SDWebImage的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 UIImage上的圆角

Use completion block and GCD to change image view in main thread after applying the new image. 应用新图像后,使用完成块和GCD更改主线程中的图像视图。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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