SDWebImage可以設(shè)置UIButton的Image,方法是
[menuButton sd_setImageWithURL:[NSURL URLWithString:imageURL] forState:UIControlStateNormal];
但是,如果imageURL的圖像,如果是2x/3x的,我們會(huì)發(fā)現(xiàn)這樣的設(shè)置方式,會(huì)導(dǎo)致Button的圖像尺寸不對(duì)。

image
實(shí)際上,sd_setImageWithURL的方式是默認(rèn)按照1x來(lái)加載圖片的。SDWebImage也無(wú)法判斷你的圖像是多少x的。
我們需要額外做一個(gè)處理。
[menuButton sd_setImageWithURL:[NSURL URLWithString:imageURL] forState:UIControlStateNormal completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
UIImage *refined = [UIImage imageWithCGImage:image.CGImage scale:scale orientation:image.imageOrientation];
[menuButton setImage:refined forState:UIControlStateNormal];
}];
這里scale需要和提供圖片的人員商定(一般是3,這樣不同設(shè)備上效果最好),如果是3x的話(huà),就填3.0。
這樣出來(lái)的效果就是:

image.png