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

时间:2014-08-26 16:46:54

标签: ios image uibutton xcode5 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本身来解决这个问题。

2 个答案:

答案 0 :(得分:0)

您是否尝试在SDWebImage的api中以另一种方法的完成块完成视图?

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


如果仍然无法工作,您可以随时首先舍入下载的图像。检查这个问题:

Rounded Corners on UIImage

答案 1 :(得分:0)

使用完成块和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];
    });
}];