美文网首页iOS开发知识小集
iOS 实现使用.9图片

iOS 实现使用.9图片

作者: 邓布利多教授 | 来源:发表于2019-06-07 16:42 被阅读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];

相关文章

网友评论

    本文标题:iOS 实现使用.9图片

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