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

时间:2013-09-13 15:18:29

标签: iphone ios objective-c ipad uiimageview

我的背景来自Default-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中的第一个语句,与最后的return语句的结果没有明显的变化:背景的简短视图,后面是白色屏幕。 note.png位于“支持文件”中。

您是否看到任何可以更改内容的问题,以便显示缩小版本的音符(保持宽高比)? note.jpg图像没有白色像素。

谢谢,

- 编辑 -

关于第一条评论:

@implementation CJSHCardView : UIImageView

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

这会回答你的问题吗?

- 第二次编辑 -

感谢您的回复和您的代码,Nick。我稍微修改了它以适应ARC并且最初设置了一个固定宽度的比例,但是我有点不确定放入它的方法。我尝试了ViewController的viewDidLoad()方法,但这导致背景显示没有隐藏或头发的笔记。我目前的viewDidLoad()方法读取:

- (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];
}

谢谢,

1 个答案:

答案 0 :(得分:2)

不需要对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];
}