UIImage如何调整与capInsets的泡沫图像?

我有像图像的泡沫,我需要dynamicresize。 示例附件。 我将有dynamic文本,我只需要调整泡沫的直线部分。 在中间留下箭头我怎样才能做到这一点? 谢谢

在这里输入图像说明

你可以使用方法:

- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight; 

这样的电话:

 UIImage* image = [[UIImage imageNamed:@"bubbleImageName.png"] stretchableImageWithLeftCapWidth:15 topCapHeight:13]; 

更改capWidth和capHeight值

我认为resizableImageWithCapInsets:将是非常有用的

例:

 UIImage *image = [[UIImage imageNamed:@"buttonImage"] resizableImageWithCapInsets:UIEdgeInsetsMake(5.0,10.0,5.0,10.0)]; [self.yourButton setBackgroundImage:image forState:UIControlStateNormal]; 

我写了一个button的例子,因为这是使用这种方法的一个常见的情况。 只玩限制值。

HTH!

编辑:

想到这个问题,写了一个小例子。 我明白了resizableImageWithCapInsets:如果我们使用二维尺度,只能保存图像的angular落。

我看到2种方法:

  1. 使用两个控件:一个用于气球,另一个用于带有适当图像的箭头
  2. 从两个图像渲染这一个控件的图像

我决定实现第二个。 在这里传递ballonView.frame作为参数:

 - (UIImage *)balloonImageWithRect:(CGRect)rect{ //create two images, ballon image with cap corners UIImage *ballonImage = [[UIImage imageNamed:@"balloon"] resizableImageWithCapInsets:UIEdgeInsetsMake(IMAGE_CAP_INSET_TOP, IMAGE_CAP_INSET_LEFT, IMAGE_CAP_INSET_BOTTOM, IMAGE_CAP_INSET_RIGHT) resizingMode:UIImageResizingModeStretch]; UIImage *arrowImage = [UIImage imageNamed:@"arrow"]; //drawing area CGSize newSize = rect.size; UIGraphicsBeginImageContext(newSize); //leave some space for arrow at the bottom [ballonImage drawInRect:CGRectMake(0, 0, newSize.width, newSize.height - arrowImage.size.height)]; //draw arrow at the bottom [arrowImage drawInRect:CGRectMake((newSize.width - arrowImage.size.width)/2, newSize.height - arrowImage.size.height, arrowImage.size.width, arrowImage.size.height)]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } 

然后我们将这个图像设置为我们的ballonView。

此外,我留下与该ViewController的完整代码与该气球的随机大小的要点: https : //gist.github.com/AlloyDev/7910637

你也可以使用这两个图像:

气球图像箭头图像

使用UIImage.h委托方法

//使用resizableImageWithCapInsets:和capInsets。

 - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight; @property(nonatomic,readonly) NSInteger leftCapWidth; // default is 0. if non-zero, horiz. stretchable. right cap is calculated as width - leftCapWidth - 1 @property(nonatomic,readonly) NSInteger topCapHeight; // default is 0. if non-zero, vert. stretchable. bottom cap is calculated as height - topCapWidth - 1 

喜欢这个

 UIImage *balloon = [[UIImage imageNamed:@"grey.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15]; 

希望这可以帮助。

对于问题中的图像(假设它是一个视网膜图像):

 [[UIImage imageNamed:@"Bubble"] resizableImageWithCapInsets:UIEdgeInsetsMake(14, 28, 14, 26) resizingMode:UIImageResizingModeStretch]; 

如果问题中提供的图像是非视网膜图像,请将值减半。

修改值( UIEdgeInsetsMake(top, left, bottom, right) )以适合您需要的任何图像。