iOS 图片拉伸

2018-08-03  本文已影响16人  iOS安年

工作中会遇到一些图片的拉伸处理,这里做一下小结

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.frame = CGRectMake(10, 10, 250, 150);
    [self.view addSubview:imageView];
    // 加载原图
    UIImage *image = [UIImage imageNamed:@"image"];
    // 拉伸核心代码
    image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(10, 30, 45, 30)
                                  resizingMode:UIImageResizingModeStretch];
    imageView.image = image;
}

1.自定义拉伸范围处理

  // UIEdgeInsetsMake(上, 左, 下, 右);
    image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>) resizingMode:<#(UIImageResizingMode)#>];

2.使图片两边不拉伸,中间拉伸

image = [image stretchableImageWithLeftCapWidth:floorf(image.size.width/2) topCapHeight:floorf(image.size.height/2)];
  1. UIImageResizeingMode 枚举
    UIImageResizeingModeTile 平铺
    UIImageResizeingModeStretch 拉伸
上一篇下一篇

猜你喜欢

热点阅读