iOS 实现使用.9图片
2019-06-07 本文已影响175人
邓布利多教授
很多时候非常羡慕安卓可以使用.9图片,不管怎么拉伸都不会变形,作为傲娇的iOS攻城狮的我坚信这种事在我大iOS里应该也是可以的,所以,找资料。最终通过不懈努力,找到了答案,终于实现了,在我大iOS中可以像安卓那样使用.9图片的方法了
官方文档api
@interface UIImage(UIImageDeprecated)
// use resizableImageWithCapInsets: and capInsets.
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight __TVOS_PROHIBITED;
@property(nonatomic,readonly) NSInteger leftCapWidth __TVOS_PROHIBITED; // default is 0. if non-zero, horiz. stretchable. right cap is calculated as width - leftCapWidth - 1
@property(nonatomic,readonly) NSInteger topCapHeight __TVOS_PROHIBITED; // default is 0. if non-zero, vert. stretchable. bottom cap is calculated as height - topCapWidth - 1@end
- 代码效果
UIImage *img = [UIImage imageNamed:@"icon_zb_comment_bg"];
img = [img stretchableImageWithLeftCapWidth:25 topCapHeight:15];
UIImageView *ivBG = [[UIImageView alloc]initWithFrame:CGRectMake(10, 100, [UIScreen mainScreen].bounds.size.width - 20, 80)];
ivBG.image = img;
[self.view addSubview:ivBG];
- 上面代码里重点的一句
img = [img stretchableImageWithLeftCapWidth:25 topCapHeight:15];