iOS开发技术

#UIButton#背景图片的拉伸

2016-08-23  本文已影响411人  冷洪林
Snip20160823_7.png
// 方法一:
- (void)viewDidLoad {
    [super viewDidLoad];
    // 0.创建一张图片
    UIImage *image = [UIImage imageNamed:@"chat_send_nor"];
    // 1.获取图片尺寸
    CGFloat width = image.size.width;
    CGFloat height = image.size.height;
    // 2.拉伸图片
    UIImage *resizableImage = [image resizableImageWithCapInsets:UIEdgeInsetsMake(height * 0.5, width * 0.5, height * 0.5 - 1, width * 0.5 - 1)];
    // 3.把拉伸过的图片设置为button的背景图片
    [self.buttonView setBackgroundImage:resizableImage forState:UIControlStateNormal];
    
}
// 方法二
- (void)viewDidLoad {
    [super viewDidLoad];  
    // 0.创建一张图片
    UIImage *image = [UIImage imageNamed:@"chat_send_nor"];
    // 1.获取图片尺寸
    CGFloat width = image.size.width;
    CGFloat height = image.size.height;
    // 2.拉伸图片
    UIImage *resizableImage = [image stretchableImageWithLeftCapWidth:width * 0.5 topCapHeight:height * 0.5];
    // 3.把拉伸过的图片设置为button的背景图片
    [self.buttonView setBackgroundImage:resizableImage forState:UIControlStateNormal];
    
}
Snip20160823_9.png
Snip20160823_12.png Snip20160823_13.png
#import "UIImage+LHLExtension.h"

@implementation UIImage (LHLExtension)

+ (instancetype)stretchableImageWithLocalName:(NSString *)imageName{
    // 0.创建一张图片
    UIImage *image = [UIImage imageNamed:imageName];
    // 1.获取图片尺寸
    CGFloat width = image.size.width;
    CGFloat height = image.size.height;
    // 2.拉伸图片
    UIImage *resizableImage = [image stretchableImageWithLeftCapWidth:width * 0.5 topCapHeight:height * 0.5];
    return resizableImage;
}
@end
 UIImage *image = [UIImage stretchableImageWithLocalName:@"chat_send_nor"];
 [self.buttonView setBackgroundImage:image forState:UIControlStateNormal];
}
Snip20160823_14.png
上一篇下一篇

猜你喜欢

热点阅读