【问题标题】:iPhone: How to extend a repeatable UIImage?iPhone:如何扩展可重复的 UIImage?
【发布时间】:2011-07-28 10:06:13
【问题描述】:

我有一张图片,我用它来构筑一条推文。它由两个圆形矩形组成,左上角有一个推特图标。重要的部分是它是可重复的,因为您可以垂直复制中间部分的任何部分,它会是相同的,只是更长。这是我创建的图像:

我的问题是,在代码中,我如何根据 UITextView 中有多少行来扩展(或缩小)它?像这样获得大小的东西:

float requiredHeight = lines * 14;

我知道这是可能的,因为苹果用他们的 SMS 应用程序做到了 :)


更新:这是执行此操作的完整代码:

UIImage *loadImage = [UIImage imageNamed:@"TwitPost.png"];
float w2 = loadImage.size.width/2;
float h2 = loadImage.size.height/2;
// I have now reduced the image size so the height must be offset a little (otherwise it stretches the bird!):
loadImage = [loadImage stretchableImageWithLeftCapWidth:w2 topCapHeight:h2+15];
imageView.image = loadImage;

感谢您的两个回答。

【问题讨论】:

    标签: iphone objective-c ios uiimageview uiimage


    【解决方案1】:

    通过使用

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

    您将 leftCapWidth 和 topCapHeight 设置为图像宽度和高度的一半。您可以通过更改其边界/框架在 UIImageView 中拉伸此图像。

    【讨论】:

    • 所以如果你用这个可拉伸的图像加载一个 UIImageView,你可以简单地改变那个 UIImageView 的框架,它会改变,并且不会尝试缩放它或任何东西?
    • 是的。您需要将图像模式设置为“缩放填充”,但实际上边框不会变形或拉伸,它只会重复中间像素
    • 试过 - 这不适用于缩小,但我猜你只是将图像缩小到你需要的大小,然后再放大?
    【解决方案2】:

    查看 UIImage 的文档。具体来说:

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

    这允许您使用在 leftCapWidth 或 topCapHeight 处重复部分的图像来水平或垂直拉伸它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多