簡體   English   中英

迅速/剪切背景圖片

[英]Swift / cut background image

我的背景圖片設置了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裁剪圖像,而不是縮放以適合圖像。 這不會扭曲您的圖像,但是顯然您將看不到整個東西。

您需要確保在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)
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM