适应image 拉伸不变形

2023-01-11  本文已影响0人  林希品

swift

let image = UIImage(named: "home_backMoney")?.resizableImage(withCapInsets: UIEdgeInsets(top: 0, left: 53, bottom: 0, right: 10), resizingMode: .stretch)
imageView.image = image

53 和 10 是不变形距离

OC

    // 不处理的图片
    UIImageView *unHandleImg = [[UIImageView alloc] initWithFrame:CGRectMake((SCREENWIDTH-200)/2, 100, 200, 30)];
    unHandleImg.image = [UIImage imageNamed:@"theImage"];
    [self.view addSubview:unHandleImg];
    
    // 处理区域拉伸的图片
    UIImageView *handleImg = [[UIImageView alloc] initWithFrame:CGRectMake((SCREENWIDTH-200)/2, 200, 200, 30)];
    UIImage *img = [UIImage imageNamed:@"theImage"];
    // 四个数值对应图片中距离上、左、下、右边界的不拉伸部分的范围宽度
    img = [img resizableImageWithCapInsets:UIEdgeInsetsMake(35, 35, 35, 35) resizingMode:UIImageResizingModeStretch];
    handleImg.image = img;
    [self.view addSubview:handleImg];
上一篇下一篇

猜你喜欢

热点阅读