contentView背景图片拉伸

在做聊天界面的时候需要对聊天内容的背景做拉伸,但是一般都是有箭头的不规则的图片,正常拉伸肯定会使图片变形。那要怎么处理呢?

拉伸图片的某个区域
// UIImage方法
// 只用设置capInsets以及UIImageResizingMode
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode NS_AVAILABLE_IOS(6_0); 
// the interior is resized according to the resizingMode

/* UIImage will implement the resizing mode the fastest way possible while
 retaining the desired visual appearance.
 Note that if an image's resizable area is one point then UIImageResizingModeTile
 is visually indistinguishable from UIImageResizingModeStretch.
 */
typedef NS_ENUM(NSInteger, UIImageResizingMode) {
    UIImageResizingModeTile,
    UIImageResizingModeStretch,
};

来个栗子

红色为图片大小,蓝色为拉伸区域_capInsets:UIEdgeInsetsMake(18,25,17,25)
    _msgBGImg.image = [[[UIImage imageNamed:@"LecHisBG"]
                        resizableImageWithCapInsets:UIEdgeInsetsMake(7, 9, 7, 10) resizingMode:UIImageResizingModeStretch]
                       imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,251评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,471评论 25 708
  • 返回一张受保护且被拉伸的图片 应用场景:聊天窗口的气泡 方法一(弃用): iOS 5.0以前使用(弃用)这个方法会...
    林安530阅读 15,955评论 1 36
  • 其实我想说说这本书是在我生日那天,和君九届银川现场班同志赠与我的生日礼物,很喜欢,尤其读完之后更喜欢!仅此感谢我亲...
    小太阳_Claire阅读 1,526评论 0 0
  • 回望2016年,其实差不多还能拿到一个 B+ 或者 A- 的分数。 相比于前年直接在家里颓了半年多时间,去年一直都...
    张文博阅读 163评论 0 0