快速/切割背景图像

时间:2016-02-24 20:13:57

标签: ios swift

我的背景图片是使用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完成这项工作,但因为它是一张照片,看起来很糟糕。

非常感谢帮助。

1 个答案:

答案 0 :(得分:1)

您可以使用.ScaleAspectFill裁剪图像,而不是缩放以适合图像。这不会歪曲你的形象,但显然你无法看到整个事物。

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

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)
    }
相关问题