0

我的背景来自 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;
}

这是否回答你的问题?

--第二次编辑--

谢谢你的回复和你的代码,尼克。我稍微改变了它以适应 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];
}

谢谢,

4

1 回答 1

2

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

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

- (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];
}
于 2013-09-13T15:49:31.837 回答