我正在为我的应用设置随机背景图片,方法是加载图片并将其添加到我的视图中:
UIImageView *background = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:
[NSString stringWithFormat:@"bg%u.jpg", 1+arc4random_uniform(15)]]];
[self.view addSubview:background];
[self.view sendSubviewToBack:background];
我的图像在326 ppi时为640x1136,但由于某种原因图像显示为放大。关于为什么会受到赞赏的任何想法。
模拟器
实际图片:
感谢。
答案 0 :(得分:1)
这是因为您使用图像分配init,而不是使用修复大小。
int screenHeight = [UIScreen mainScreen].bounds.size.height;
int screenWidth = [UIScreen mainScreen].bounds.size.width;
UIImageView *background = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
background.image = [UIImage imageNamed:[NSString stringWithFormat:@"bg%u.jpg",1+arc4random_uniform(15)]]
[self.view addSubview:background];
[self.view sendSubviewToBack:background];
这样做
答案 1 :(得分:0)
你喜欢这样,
UIImageView *background = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:
[NSString stringWithFormat:@"bg%u.jpg", 1+arc4random_uniform(15)]]];
background.frame = self.view.frame; // this is the only line you have missed
[self.view addSubview:background];
[self.view sendSubviewToBack:background];