【问题标题】:how to scale to fit the background image objective c如何缩放以适合背景图像目标c
【发布时间】:2017-06-06 07:01:26
【问题描述】:

我正在尝试在没有任何 uiimageview 的情况下设置图像,现在我有了图像,但它水平重复了两次我想缩放并将其修复为屏幕大小你怎么做??

我尝试了一些方法,但没有成功

下面是我的代码:

- (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.
}

提前感谢您的建议!!

【问题讨论】:

  • 简单这一行就够了self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background_themee"]];
  • @GeneCode 好的,让我试试吧!

标签: ios objective-c xcode background


【解决方案1】:

colorWithPattern 将始终在视图上重复图像。如果你在 iPhone 中打开,它可能会出现 2 次图像。如果你在 iPad 上运行,可能会显示 4 张图像。所以不适合做背景图。

您需要的是一个 UIImageView,其约束设置正确,contentMode 设置为 AspectFill。

【讨论】:

  • 是的,谢谢!如果您认为这是一个有效的问题,您可以投票:)
【解决方案2】:

您可以根据需要选择要填充的比例或纵横比填充/适合

【讨论】:

    猜你喜欢
    • 2015-07-14
    • 1970-01-01
    • 2021-03-23
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多