【问题标题】:Setting Background image to a uiview将背景图像设置为 uiview
【发布时间】:2015-01-24 14:25:48
【问题描述】:

我将视图的背景设置为图像。为此使用以下代码。但是图像似乎被拉长了。我怎样才能得到原来的图像?

self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"image.png"]];

【问题讨论】:

标签: ios uiimage uicolor


【解决方案1】:

不建议像这样设置UIView 背景颜色(设置图像),而是添加UIImageView 作为UIView 的子视图,然后将图像分配给它。这看起来就像您设置了背景一样。

但是,这是解决方案,

UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:@"image.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

self.view.backgroundColor = [UIColor colorWithPatternImage:image];

直接来自this answer

【讨论】:

    【解决方案2】:
    UIGraphicsBeginImageContext(self.view.frame.size);
    [[UIImage imageNamed:@"myImage.png"] drawInRect:self.view.bounds];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.view.backgroundColor = [UIColor colorWithPatternImage:image];
    

    试试这个代码。它对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-12
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 2014-06-23
      相关资源
      最近更新 更多