【问题标题】:stretchableImageWithLeftCapWidth:topCapHeight doesn't work in initWithCoder: of UIImageView subclassstretchableImageWithLeftCapWidth:topCapHeight 在 initWithCoder: 的 UIImageView 子类中不起作用
【发布时间】:2009-08-13 20:38:32
【问题描述】:

我有一个名为 ShadowView 的 UIImageView 子类,它显示可以在任何东西下使用的阴影。 ShadowViews 将从笔尖加载。

在 initWithCoder: 中,我有以下代码:

- (id)initWithCoder:(NSCoder *)decoder {
    self = [super initWithCoder:decoder];
    if (self != nil) {
        UIImage *shadowImage = [[UIImage imageNamed:@"drop_shadow_4_pix.png"] stretchableImageWithLeftCapWidth:4 topCapHeight:4];

        [self setContentMode:UIViewContentModeScaleToFill];
        [self setImage:shadowImage];
    }
    return self;
}

但是,当我运行该应用程序时,该图像不会出现。

但是如果我把它改成

...
UIImage *shadowImage = [UIImage imageNamed:@"drop_shadow_4_pix.png"];
...

它工作正常,但拉伸错误。

关于为什么会发生这种情况的任何想法?

编辑:当我以编程方式加载 shadowview 时也是一样的,initWithFrame: 的实现与 initWithCoder: 类似。

另一个编辑:我想我解决了这个问题。我需要设置自动调整大小的蒙版。

【问题讨论】:

    标签: iphone objective-c uiimageview uiimage


    【解决方案1】:

    shadowImage 为零吗?

    UIImage *shadowImage = [[UIImage imageNamed:@"drop_shadow_4_pix.png"] stretchableImageWithLeftCapWidth:4 topCapHeight:4];
    

    如果基础图像的宽度小于 5 像素或高度小于 5 像素,该方法可能会返回 nil,因为它需要 4 像素来拉伸大写字母 + 1 像素。

    【讨论】:

    • 9 x 9 像素,我想。居中的行和列,每边都有大写字母。
    猜你喜欢
    • 2013-10-08
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多