1 图片拉伸

作者: 小码码 | 来源:发表于2017-03-22 15:19 被阅读40次

    处理图片拉伸的方式有很多,以下参考了http://www.jianshu.com/p/80290e6ae9ac, http://www.jianshu.com/p/1110109f43f5
    两篇文章的分享,总结如下,仅为自己使用方便.

    1 代码方式

    1.1 iOS 5.0之前

    • (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;
     UIImage *image = [UIImage imageNamed:@"chat"];
        //  1 =  width - leftCapWidth - right
        //   1 =  height - topCapWidth - bottom
        UIImage *reszingImage = [image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.5];
    
     [self.btn setBackgroundImage: reszingImage forState:UIControlStateNormal];
    

    1.2 iOS 5.0

    • (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets;
    // 默认是平铺
        UIImage *resizingImage = [image resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
    

    1.3 iOS 6.0

    • (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode;
     // 1.创建图片对象
        UIImage *image = [UIImage imageNamed:@"chat"];
        // 2.创建可拉伸的图片(告诉图片什么地方需要拉伸)
        CGFloat imageW = image.size.width;
        CGFloat imageH = image.size.height;
    
          /*UIImageResizingModeStretch:拉伸模式,通过拉伸UIEdgeInsets指定的矩形区域来填充图片
    UIImageResizingModeTile:平铺模式,通过重复显示UIEdgeInsets指定的矩形区域来填充图片*/
        UIImage *resizingImage = [image resizableImageWithCapInsets:UIEdgeInsetsMake(imageH * 0.5, imageW * 0.5, imageH * 0.5 - 1, imageW * 0.5 - 1) resizingMode:UIImageResizingModeTile];
    

    2 图形化方法

    2.1 storyBoard中

    2.2 assets中show slicing

    相关文章

      网友评论

      本文标题:1 图片拉伸

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