【问题标题】:Why would set view.layer.contents instead of using an UIImage为什么要设置 view.layer.contents 而不是使用 UIImage
【发布时间】:2018-03-12 16:53:16
【问题描述】:

最近遇到这个代码:-

self.view.layer.contents = (id)[UIImage imageNamed:@"img_bg.png"].CGImage;

这为UIViewController 的整个视图设置了背景。

通常,我只需将UIImageView 设置为子视图,从UIViewController 获取视图的整个区域

为什么有人会使用一种技术而不是另一种?

我觉得这个有点令人不安,因为故事板中没有显示背景;我期待的地方。

【问题讨论】:

    标签: ios uiimageview core-image


    【解决方案1】:

    这是一个很好的问题。深入地,您在问 View 和 Layer 之间的区别,您知道我们可以将 View 视为层的委托,它可以处理用户与应用程序或系统的交互。当除了渲染视图没有任何交互时,我们可以只使用图层,与视图相比占用内存更少。

    【讨论】:

      【解决方案2】:

      试试这个

      self.view.backgroundColor = UIColor(patternImage: UIImage(named:"img_bg.png"))
      

      希望这会有所帮助 干杯

      【讨论】:

      • 对不起,这不是我问的。两种代码都可以正常工作;我只是想知道它们的区别。
      【解决方案3】:

      你可以试试这个:-

      UIGraphicsBeginImageContext(self.view.frame.size)
      UIImage(named: "img_bg.png")?.drawInRect(self.view.bounds)
      let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
      UIGraphicsEndImageContext()
      self.view.backgroundColor = UIColor(patternImage: image)
      

      希望这会有所帮助, 谢谢

      【讨论】:

      • 对不起,这不是我问的。两种代码都可以正常工作;我只是想知道它们的区别。
      猜你喜欢
      • 1970-01-01
      • 2021-09-23
      • 2013-01-04
      • 2013-11-27
      • 2013-12-07
      • 1970-01-01
      • 2018-10-25
      • 2016-01-09
      相关资源
      最近更新 更多