图片拉伸(聊天气泡)

2019-12-23  本文已影响0人  BestBoy

图片拉伸(聊天气泡)

图片受保护区域的图片拉伸,分别以代码跟Assets两种方式。


代码方式如下几种方式:

- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets
 UIImageResizingModeTile = 0,
 //平铺
 UIImageResizingModeStretch = 1,
 //拉伸
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight

如果项目中经常使用可以增加一个扩展类

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface UIImage (YZImage)

+(instancetype)reziableImageWithImageName:(NSString *)imageName;
@end

NS_ASSUME_NONNULL_END
#import "UIImage+YZImage.h"

@implementation UIImage (YZImage)

+(instancetype)reziableImageWithImageName:(NSString *)imageName{
    
    UIImage *image = [UIImage imageNamed:imageName];
    CGFloat imageWidth = image.size.width;
    CGFloat imageHeight = image.size.height;
    
    return [image stretchableImageWithLeftCapWidth:imageWidth*0.5 topCapHeight:imageHeight*0.5];
    
}

@end

非代码的方式:

上一篇 下一篇

猜你喜欢

热点阅读