iOS图片拉伸

2021-06-23  本文已影响0人  新生代农民工No1

前言:在开发工程中,设计给的图不能完全满足我们的需求,所以有时就需要我们根据需求自己去处理;

整体拉伸:
通过设置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属性的单位是比例(而不是绝对坐标)。

上一篇 下一篇

猜你喜欢

热点阅读