【问题标题】:UIImageView autoresizingmask, UIImage stretchableImageWithLeftCapWidthUIImageView autoresizingmask,UIImage 可拉伸ImageWithLeftCapWidth
【发布时间】: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


    【解决方案1】:

    实际上,您不需要创建三个图像。可拉伸 UIImage 的要点是它拉伸了中间而不是用户可定义的末端。您以像素为单位定义不想被拉伸的水平和垂直区域。

    用起来真的很简单,举个例子:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-11
      • 2011-09-19
      相关资源
      最近更新 更多