【发布时间】:2013-10-04 01:17:52
【问题描述】:
我有:
[一系列 if 语句将 backgroundImage 设置为 [UIImageNamed @"Background-Default-Landscape"];如果是 Retina 显示器,我是否可以假设设备会查找名为 Background-Default-Landscape@2x.png 的文件?]
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
CGRect containerRect = CGRectZero;
containerRect.size = [backgroundImage size];
UIView *containerView = [[UIView alloc] initWithFrame:containerRect];
[containerView addSubview:backgroundView];
现在我的应用程序正在启动,加载后会显示一个白色屏幕,而不是指定的背景(没有一个背景是纯白色的)。
在我开始将东西放在背景上之前,我缺少什么来绘制背景,在代码的下方?
谢谢,
--编辑--
就代码而言,我有:
- (void) renderScreen
{
int height = [[UIScreen mainScreen] bounds].size.height;
int width = [[UIScreen mainScreen] bounds].size.width;
UIImage *backgroundImage = nil;
if (height == 2048 || height == 2008 || height == 1024 || height == 1004 || height == 984)
{
backgroundImage = [UIImage imageNamed:@"Background-Default-Portrait"];
}
// Other such statements cut.
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
CGRect containerRect = CGRectZero;
containerRect.size = [backgroundImage size];
UIView *containerView = [[UIView alloc] initWithFrame:containerRect];
[containerView addSubview:backgroundView];
[self.view addSubview:containerView];
然后下面是要在背景上绘制的语句。
在初始视图加载和旋转时调用此方法:
- (void)viewDidLoad
{
[super viewDidLoad];
[self renderScreen];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(renderScreen) name:UIDeviceOrientationDidChangeNotification object:nil];
}
行为是加载了正确的初始屏幕,然后它变成白色,之后似乎没有任何有趣的事情发生。
--编辑--
在顶部,我有:
int height = [[UIScreen mainScreen] bounds].size.height;
int width = [[UIScreen mainScreen] bounds].size.width;
当我 NSLog 高度和宽度时,对于处于横向模式的视网膜设备,我得到高度为 1024 和宽度为 768。显示的图像是纵向图像的旋转;如果我把设备放在一边,图像会整齐地填满整个屏幕,但背景会显示水平压缩的图像。
为了正确获取高度和宽度,我应该做些什么不同的事情吗?我希望横向设备的高度为 768,宽度为 1024。
【问题讨论】:
-
这一切似乎都是正确的。我们需要查看更多代码。您在哪种方法中添加视图?您要向哪个视图添加
containerView? -
你能放更多代码吗?我假设您正在将 containerView 添加到 self.view。非视网膜图像也是 Default-Background-Labdscape.png(带有 .png 扩展名)。
-
@GuyKogus,谢谢;我在代码中添加了更多上下文。
-
@JonathanHayward 在代码中添加图像扩展名并尝试。在这段代码中 backgroundImage = [UIImage imageNamed:@"Background-Default-Portrait"];
-
为什么要使用设备方向通知?如果您使用的是
UIViewController类,则使用给定的界面方向更改方法。另外,使用CGFloat而不是int作为宽度和高度。与您的数据类型保持一致! :)
标签: iphone ios ipad uiimageview background-image