设置图片显示的状态,有这么几个属性.根据情况具体使用
typedef NS_ENUM(NSInteger, UIViewContentMode) {
UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit, // contents scaled to fit with fixed aspect. remainder is transparent
UIViewContentModeScaleAspectFill, // contents scaled to fill with fixed aspect. some portion of content may be clipped.
UIViewContentModeRedraw, // redraw on bounds change (calls -setNeedsDisplay)
UIViewContentModeCenter, // contents remain same size. positioned adjusted.
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
};
如果我们使用的是使用的 imageView ,
imageView.contentMode = UIViewContentModeScaleAspectFill;
这样就能达到我们的效果,但是如果使用的是button.要满足三个条件
用 button.imageView.contentMode 而不是 button.contentMode
打开 clipsToBounds 属性. 设置为 YES
设置的是 image 而不是 backGroundImage ,设置 backGroundImage 没有效果.
button.imageView.contentMode = UIViewContentModeScaleAspectFill;
button.clipsToBounds = YES;
[button sd_setImageWithURL:[NSURL URLWithString:imag1] forState:UIControlStateNormal];