UIImageView autoresizingmask,UIImage stretchableImageWithLeftCapWidth

时间:2012-01-31 06:02:19

标签: iphone uiimageview uiimage

我正在观看一些WWDC视频,他们谈论使用UIImageView来拉伸你的图像而不会产生任何额外的记忆。他们的例子是使用一个小文本泡泡,就像你在音乐应用程序中长按tableViewCell时得到的那个。然后他们讨论如何将它分成3个切片,左,右和中心,因为中心部分底部有一个小三角形,指向你长按的tableViewCell。有人可以解释某人是如何做到的,因为他们没有在他们的例子中提供任何代码吗?伪代码很好。看一下文档,我的猜测是:

从图形编辑器中创建三个最小尺寸的UIImages:

UIImage *leftImage = [UIImage imageNamed:@"left.png"];
UIImage *rightImage = [UIImage imageNamed:@"right.png"];
UIImage *centerImage = [UIImage imageNamed:@"center.png"];

leftImage = [leftImage stretchableImageWithLeftCapWidth:0 topCapHeight:0];
rightImage = [rightImage stretchableImageWithLeftCapWidth:0 topCapHeight:0];
centerImage = [centerImage stretchableImageWithLeftCapWidth:0 topCapHeight:0];

UIImageView *leftImgView = [[UIImageView alloc] initWithImage:leftImage];
leftImgView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

UIImageView *rightImgView = [[UIImageView alloc] initWithImage:rightImage];
leftImgView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

UIImageView *centerImgView = [[UIImageView alloc] initWithImage:centerImage];
leftImgView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

// do I just add them all to the subview of self.view?  Do I need to set their frames somewhere?  If so, how do I know where they go since it's 3 slices making up an image.
// release memory

我对此主题的相关问题是,您将如何在IB中执行此操作。就像你让3个imageViews占据整个屏幕一样,因为那是你想要的图像扩展到什么?

1 个答案:

答案 0 :(得分:2)

实际上,您不需要创建三个图像。可伸展UIImage的关键在于它伸展中间但不是用户可定义的末端。您可以定义不希望被拉伸的水平和垂直区域。

使用起来非常简单,这是一个例子:

UIImage *stretchyImage = [image stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0]

然后将其设置为您想要的大小。带有插图的精美文档是here

编辑: 我认为您必须以编程方式创建可伸缩图像,请参阅here