gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:26:34 25 4
gpt4 key购买 nike

我的背景来自 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 语句相比没有明显的变化:背景的简要 View ,然后是白色屏幕。 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];
}

谢谢,

最佳答案

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

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

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

关于iphone - 为什么我尝试在背景上显示图像会导致显示白色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18789822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com