图片拉伸共有三种方法:

作者: Just_go | 来源:发表于2017-07-07 14:13 被阅读24次
    - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight
    示例:
        // 左端盖宽度
        NSInteger leftCapWidth = image.size.width * 0.5f;
        // 顶端盖高度
        NSInteger topCapHeight = image.size.height * 0.5f;
        // 重新赋值
        image = [image stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];
    
    
    - (UIImage *)resizableImageCapInsets:(UIEdgeInsets)Insets
    
        // 重新赋值
        CGFloat top = 30; // 顶端盖高度
        CGFloat bottom = 30 ; // 底端盖高度
        CGFloat left = 35; // 左端盖宽度
        CGFloat right = 35; // 右端盖宽度
        UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
        // 伸缩后重新赋值
        image = [image resizableImageWithCapInsets:insets];
    
    - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode
    
        // 重新赋值
        CGFloat top = 30; // 顶端盖高度
        CGFloat bottom = 30 ; // 底端盖高度
        CGFloat left = 35; // 左端盖宽度
        CGFloat right = 35; // 右端盖宽度
        UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
        // 伸缩后重新赋值 UIImageResizingModeTile:平铺  UIImageResizingModeStretch:拉伸
        image = [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeTile];
    

    SWIFT

     var image = UIImage(named:"bg.png")
    // 左端盖宽度
    var leftCapWidth:Int = Int(image!.size.width * 0.5)
    // 顶端盖高度
    var topCapHeight:Int = Int(image!.size.height * 0.5)
    
    image = image!.stretchableImageWithLeftCapWidth(leftCapWidth,topCapHeight: topCapHeight)
    

    相关文章

      网友评论

        本文标题:图片拉伸共有三种方法:

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