【问题标题】:Swift / cut background imageSwift / 剪切背景图片
【发布时间】:2016-02-24 20:13:56
【问题描述】:

我的背景图片是用 UIView 扩展设置的。

extension UIView {
    func addBackground() {
        // screen width and height:
        let width = UIScreen.mainScreen().bounds.size.width
        let height = UIScreen.mainScreen().bounds.size.height

        let imageViewBackground = UIImageView(frame: CGRectMake(0, 0, width, height))
        imageViewBackground.image = UIImage(named: "index_clear")

        // you can change the content mode:
        imageViewBackground.contentMode = UIViewContentMode.ScaleAspectFill
        self.addSubview(imageViewBackground)
        self.sendSubviewToBack(imageViewBackground)
    }}

对于 segue 动画,视图从左向右移动。背景图像比屏幕大得多,动画时可见。

如何剪切背景图像以完美地融入视图? .ScaleAspectFit 完成了这项工作,但因为它是一张照片,所以看起来很糟糕。

非常感谢您的帮助。

【问题讨论】:

  • .ScaleAspectFill 不会改变纵横比,因此不会扭曲图像,但会裁剪图像中不适合的部分
  • 不,它没有。就像我说的那样,视图在带有 secondVC.frame = CGRectOffset(firstVC.frame, -screenWidth + screenWidth, 0.0) 的动画中移动,并且可以看到,背景图像大约是视图的 3 倍
  • 你在图片视图上设置clipsToBounds=true了吗?
  • 我现在做了,效果很好。谢谢您,请随时回答,以便我接受。谢谢!

标签: ios swift


【解决方案1】:

您可以使用.ScaleAspectFill 来裁剪图像,而不是将其缩放以适应。这不会扭曲您的图像,但显然您将无法看到整个画面。

您需要确保在UIImageView 上设置clipsToBounds=true,以便裁剪区域不可见

extension UIView {
    func addBackground() {
        // screen width and height:
        let width = UIScreen.mainScreen().bounds.size.width
        let height = UIScreen.mainScreen().bounds.size.height

        let imageViewBackground = UIImageView(frame: CGRectMake(0, 0, width, height))
        imageViewBackground.image = UIImage(named: "index_clear")

        // you can change the content mode:
        imageViewBackground.contentMode = UIViewContentMode.ScaleAspectFill
        imageViewBackground.clipsToBounds=true
        self.addSubview(imageViewBackground)
        self.sendSubviewToBack(imageViewBackground)
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-25
    • 2022-11-17
    • 2016-01-19
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 1970-01-01
    • 2015-08-30
    相关资源
    最近更新 更多