如何在iOS中切片图像和伸缩

时间:2016-09-02 05:17:08

标签: ios objective-c uiimage slice

我需要拉伸图像左右中心半圆保持原样。中心还需要半圈

enter image description here

我尝试过切片概念,并尝试下面的代码

UIImage *image = self.imgBGBottom.image;
CGFloat capWidth =  floorf(image.size.width / 2) - 50;
CGFloat capHeight =  0;
UIImage *capImage = [image resizableImageWithCapInsets:
                     UIEdgeInsetsMake(capHeight, capWidth, capHeight, capWidth)];

[self.imgBGBottom setImage:capImage];

但它对我不起作用

请帮帮我。提前谢谢。

2 个答案:

答案 0 :(得分:3)

您正在使用功能,请使用- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight代替- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets

设置顶部左帽以拉伸图像,如下面的代码所述。

<强>目标C

UIImage *image = [UIImage imageNamed:@"test"];

CGFloat capTop =  50;  // top cap to keep half round as is
CGFloat capLeft = 5; // To repeat it or stretch equally.
UIImage *capImage = [image stretchableImageWithLeftCapWidth:capLeft topCapHeight:capTop];

<强>夫特

let image =  UIImage(named: "stretchableImage")

let capTop:Int =  50;  // top cap to keep half round as is
let capLeft:Int = 5; // To repeat it or stretch equally.
let capImage = image?.stretchableImage(withLeftCapWidth: capLeft, topCapHeight: capTop)

Alternet Solution

使用以下功能也可以实现相同的结果。

<强>目标C

UIImage *stretchedImage = [image resizableImageWithCapInsets:
                 UIEdgeInsetsMake(50, 50, 0, 50)];

<强>夫特

var stretchedImage = image?.resizableImage(withCapInsets: UIEdgeInsets(top: 50, left: 50, bottom: 0, right: 50), resizingMode: .stretch) 
  

注意:保持可伸缩图像尽可能小,否则无法使用较小的图像容器(ImageView,Button等)正确拉伸。你可以减少高度和现有图像的宽度。

答案 1 :(得分:0)

试试这个

ubuntu:latest top