【发布时间】:2012-01-31 06:02:18
【问题描述】:
我正在观看一些 WWDC 视频,他们谈论使用 UIImageView 来拉伸图像以不产生任何额外的内存。他们的示例是使用一个小文本气泡,就像您在 Music 应用程序中长按 tableViewCell 时得到的那样。然后他们讨论了如何将它分成 3 个切片,左、右和中心,因为中心部分底部有一个小三角形,指向你长按的 tableViewCell。由于他们在示例中没有提供任何代码,有人可以解释一下他们是如何做到这一点的吗?伪代码很好。查看文档,我的猜测是:
从您的图形编辑器中创建三个最小尺寸的 UIImage:
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 占据了整个屏幕,因为这是您希望图像扩展到的内容?
【问题讨论】:
标签: iphone uiimageview uiimage