繁体   English   中英

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

[英]Show UIButton image as round with SDWebImage

我的代码曾经在切换到SDWebImage之前可以工作。 但是现在它不起作用了。 我的意思是,图像显示为正方形而不是圆形。 这是我的代码

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

我宁愿不必进入并更改SDWebImage本身来解决此问题。

您是否尝试过从SDWebImage的api的另一种方法的完成块中舍入视图?

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


如果仍然无法解决问题,您可以始终先将下载的图像四舍五入。 检查这个问题:

UIImage上的圆角

应用新图像后,使用完成块和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