2012-01-31 99 views
0

我正在看一些WWDC视频,他们谈论使用UIImageView来拉伸你的图像,不会产生任何额外的内存。他们的例子是使用一个小文本泡泡,就像您在Music应用程序中长按tableViewCell时所获得的泡泡一样。然后他们会谈论如何将它分成3片,左边,右边和中间,因为中间部分底部有一个小三角形,指向您长时间按下的tableViewCell。有人可以解释某人如何做这件事,因为他们没有在他们的例子中提供任何代码?伪代码很好。综观文档,我的猜测是:UIImageView autoresizingmask,UIImage stretchableImageWithLeftCapWidth

从图形编辑器创建的最小尺寸三个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占据整个屏幕,因为这是你想要的图像扩大到?

回答

2

其实,你不需要创建三个图像。可伸缩UIImage的一点是它延伸了中间,但不是用户可定义的结束。您可以定义不想伸展的像素的水平和垂直区域。

这是非常简单的使用,这里有一个例子:

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

然后,只需将其设置为你想要的大小。带插图的好文档是​​。

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

相关问题