[英]how to scale to fit the background image objective c
I am trying to set the image without any uiimageview,now I have the image but it is repeated horizontally twice I want to scale and fix it to screen size how do u do that?? 我正在尝试在没有任何uiimageview的情况下设置图像,现在我有图像了,但是水平重复了两次,我想缩放并将其固定到屏幕尺寸,该怎么办?
I have tried some methods but nothing worked out 我尝试了一些方法,但没有解决
below is my code: 下面是我的代码:
- (void)viewDidLoad {
[super viewDidLoad];
UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:@"background_themee.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// self.view.backgroundColor = [UIColor colorWithPatternImage:image];
// UIGraphicsBeginImageContext(self.view.frame.size);
// [[UIImage imageNamed:@"background_themee.png"] drawInRect:self.view.bounds];
// UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
// UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background_themee"]];
// Do any additional setup after loading the view.
}
thanks in advance for your suggestions!! 在此先感谢您的建议!
colorWithPattern
will always repeat the image over the view. colorWithPattern
将始终在视图上重复图像。 If you open in iPhone, it maybe appear 2 times image. 如果在iPhone中打开,它可能会出现2倍的图像。 And if you run on iPad, there might show 4 image. 如果在iPad上运行,可能会显示4张图像。 So it is not good for background image. 因此对背景图像不利。
What you need is a UIImageView with contraints properly set and contentMode set to AspectFill. 您需要的是一个UIImageView,其中正确设置了约束并将contentMode设置为AspectFill。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.