美文网首页
iOS开发中图片拉伸适应的几种方式

iOS开发中图片拉伸适应的几种方式

作者: 加盐白咖啡 | 来源:发表于2019-10-01 00:24 被阅读0次
    // 创建图片对象
    UIImage *image = [UIImage imageNamed:@"chat_send_nor"];
    // 获取图片的宽高
    CGFloat width = image.size.width;
    CGFloat height = image.size.height;
    // 设置拉伸区域
    // 方式一 iOS5.0才有这个 resizableImageWithCapInsets)
    /**
     上:图片高度*0.5
     左:图片宽度*0.5
     下:图片高度*0.5-1(留1个像素拉伸)
     右:图片宽度*0.5-1(留1个像素拉伸)
    */
    UIImage *resIamge1 = [image resizableImageWithCapInsets:UIEdgeInsetsMake(height * 0.5, width * 0.5, height * 0.5 - 1, width * 0.5 - 1)];
    
    // 方式二(iOS 6 以后多一个参数拉伸模式)
    // 同方式一一样的原理,就是多一个参数
    // UIImageResizingModeTile       平铺模式(默认)
    // UIImageResizingModeStretch    拉伸模式
    UIImage *resImage2 = [image resizableImageWithCapInsets:UIEdgeInsetsMake(height * 0.5, width * 0.5, height * 0.5 - 1, width * 0.5 - 1) resizingMode:UIImageResizingModeTile];
    
    // 方式三( iOS 5 以前的方法)
    // 右边需要保护的区域 = 图片宽度width - 左边保护区域leftCapWidth - 1
    // 底部需要保护的区域 = 图片高度height - 顶部保护区域topCapHidth - 1
    UIImage *resImage3 = [image stretchableImageWithLeftCapWidth:width * 0.5 topCapHeight:height * 0.5];
    [self.buutton setBackgroundImage:resImage3 forState:UIControlStateNormal];
    
    // 方式四
    /*
        直接在Assets里设置图片属性,Slices,水平和垂直方向的拉伸,自动算出区域
        如果是特殊形状的图片,比如气泡,需要自己手动调节一下坐标
     */

相关文章

网友评论

      本文标题:iOS开发中图片拉伸适应的几种方式

      本文链接:https://www.haomeiwen.com/subject/sdtkpctx.html