前言:在开发工程中,设计给的图不能完全满足我们的需求,所以有时就需要我们根据需求自己去处理;
整体拉伸:
通过设置UIImageView
的属性contentMode;
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,
};
局部拉伸:
局部拉伸有几种方法,一种是对图片本身去做拉伸,另一种是对Layer层去做拉伸;(可能还有其他的,目前只考虑这两种)
UIImage
的拉伸方法:
/**
capInsets:拉伸的范围
resizingMode:拉伸的模式 常用 UIImageResizingModeStretch
*/
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode NS_AVAILABLE_IOS(6_0);
// the interior is resized according to the resizingMode
CALayer
的拉伸方法:
通过设置CALayer
的属性contentsCenter来指定拉伸区域;contentsCenter是CGRect
类型,默认值为(0,0,1,1);
/**
拉伸范围
*/
@property CGRect contentsCenter;
注意CALayer的contentsRect属性的单位是比例(而不是绝对坐标)。
网友评论