为什么我尝试在背景上显示图像会导致白色显示



我有一个来自默认568h@2x.png的背景。我想在部分背景上显示缩小的 PNG,但调用:

[[CJSHCardView alloc] initWithScale:.1];

将显示变为白色。如果我注释这一行,这不会发生,但是在函数调用中,即使我立即返回它,它也会在半秒后使显示变为白色:

- (id)initWithScale:(float)scale
{
    UIImage *img = [UIImage imageNamed:@"note.png"];
    self = [super initWithImage:img];
    if (self != nil)
    {
        self.frame = CGRectMake(0, 0, img.size.width * scale, img.size.height * scale);
        UIImageView *myImage = [[UIImageView alloc] initWithFrame:self.frame];
        myImage.opaque = NO;
        [self addSubview:myImage];
    }
    return self;
}

移动/复制 return 语句以成为 initWithScale 中的第一个语句,与末尾正确返回语句的方式相比没有明显的变化:背景的简要视图,然后是白屏。 注意.png在"支持文件"中。

您是否看到我可以更改内容以显示缩小版本的注释(保持纵横比)的任何陷阱?注意.jpg图像没有白色像素。

谢谢

--编辑--

关于第一条评论:

@implementation CJSHCardView : UIImageView
- (id)initWithFrame:(CGRect)frame
{
    NSAssert(NO, @"Call initWithHeight instead.");
    return self;
}

这能回答你的问题吗?

-

-第二次编辑--

谢谢你的回复和你的代码,尼克。我稍微修改了它以适应 ARC 并最初设置了一个固定宽度的比例,但我有点不确定把它放进去的方法。我尝试了ViewController的viewDidLoad()方法,但这导致背景显示时没有隐藏或毛发。我目前的观点DidLoad()方法如下:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIImage *backgroundImage = [UIImage imageNamed:@"Default-568h"];
    CGRect containerRect = CGRectZero;
    containerRect.size = [backgroundImage size];
    UIView *containerView = [[UIView alloc] initWithFrame:containerRect];
    float scale=.1;
    UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
    [containerView addSubview:backgroundView];
    [backgroundView sizeToFit];
    // [backgroundView release];
    UIImage *noteImage = [UIImage imageNamed:@"note"];
    CGSize noteSize = [noteImage size];
    UIImageView *noteView = [[UIImageView alloc] initWithImage:noteImage];
    [noteView setContentMode:UIViewContentModeScaleAspectFit];
    [noteView setFrame:CGRectMake(0, 0, noteSize.width * scale, noteSize.height * scale)];
    [containerView addSubview:noteView];
    // [noteView release];
}

谢谢

不需要

子类化UIImageView。只需创建一个容器UIView并添加 2 个UIImageView子视图。

编辑 - 这应该适用于您的实现:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIImage *backgroundImage = [UIImage imageNamed:@"Default-568h"];
    CGRect containerRect = CGRectZero;
    containerRect.size = [backgroundImage size];
    UIView *containerView = [[UIView alloc] initWithFrame:containerRect];
    [[self view] addSubview:containerView];
    float scale=.1;
    UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
    [containerView addSubview:backgroundView];
    [backgroundView sizeToFit];
    UIImage *noteImage = [UIImage imageNamed:@"note"];
    CGSize noteSize = [noteImage size];
    UIImageView *noteView = [[UIImageView alloc] initWithImage:noteImage];
    [noteView setContentMode:UIViewContentModeScaleAspectFit];
    [noteView setFrame:CGRectMake(0, 0, noteSize.width * scale, noteSize.height * scale)];
    [containerView addSubview:noteView];
}

最新更新