zoukankan      html  css  js  c++  java
  • UIButton图片拉伸方法(很多需要按钮的地方我们只需要一张小图来进行缩放)

    系统提供3种方法来满足不同的需求(直接上代码):


    #import <UIKit/UIKit.h>
    
    @interface UIImage (Common)
    
    /**
     *  根据图片名返回一张能自由拉伸的图片(图片缩放)
     */
    + (UIImage *)resizedImage:(NSString *)name;
    
    /**
     *iOS5提供的方法
     */
    +(UIImage*)resizeImageWithIOS5Method:(NSString*)imageName;
    
    /**
     *iOS6提供的方法,从中间开始缩放
     */
    +(UIImage*)resizeImageWithIOS6Method:(NSString*)name;
    
    @end

    .m的的实现方法:

    #import "UIImage+Common.h"
    
    @implementation UIImage (Common)
    
    + (UIImage *)resizedImage:(NSString *)name
    {
        UIImage *image = [UIImage imageNamed:name];
        return [image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.5];
    }
    
    /**
     *iOS5提供的方法
     */
    +(UIImage*)resizeImageWithIOS5Method:(NSString*)imageName{
        UIImage *image = [UIImage imageNamed:imageName];
        //顶端盖的高度
        CGFloat top = 25;
        CGFloat bottom = 25;//低端盖的高度
        CGFloat left = 10;//左端盖的高度
        CGFloat right = 10;//右端盖的高度
        UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
        //伸缩后重新赋值
        return [image resizableImageWithCapInsets:insets];
    }
    
    /**
     *iOS6提供的方法
     *@param  UIImageResizingModeStretch:拉伸模式,通过拉伸UIEdgeInsets指定的矩形区域来填充图片
     *@param  UIImageResizingModeTile:平铺模式,通过重复显示UIEdgeInsets指定的矩形区域来填充图片
     */
    +(UIImage*)resizeImageWithIOS6Method:(NSString*)name{
        UIImage *image = [UIImage imageNamed:name];
        //顶端盖的高度
        CGFloat top = 10;
        CGFloat bottom = 10;//低端盖的高度
        CGFloat left = 10;//左端盖的高度
        CGFloat right = 10;//右端盖的高度
        UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
        //指定为拉伸模式,伸缩后重新赋值
        return [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
    }
  • 相关阅读:
    通信专业术语解释
    STM32F10系列管脚设置
    [笔试题]使用回调函数编写冒泡排序,可以排序整形数组,也可以排序字符串
    Date常用转换、比较
    哈希映射
    APP技巧格式
    $.get/$.post/$.ajax/$.getJSON
    使用Rss框架PHP开发流程
    测试rss与navicat连接
    验证码技术
  • 原文地址:https://www.cnblogs.com/Crazy-ZY/p/5622619.html
Copyright © 2011-2022 走看看