【问题标题】:How to apply circle mask to UIImageView如何将圆形遮罩应用于 UIImageView
【发布时间】:2016-10-30 15:46:09
【问题描述】:

我已阅读其他问题,为了将圆形遮罩应用于 UIImageView 我应该这样做:

self.imageView.layer.cornerRadius = self.imageView.frame.size.width / 2
self.imageView.clipsToBounds = true

但由于某种原因,当我这样做时,我的图像就被隐藏了。当我删除应用蒙版的代码时,图像完美显示,但应用蒙版后没有任何显示。

我不知道是不是因为 UIImageView 在另一个 UIView 中,但我不明白为什么这不起作用。

这些是我用来处理图像的文件:

import UIKit

class ChatViewController: UIViewController {

    var v: ChatOverview!

    init () {
        super.init(nibName: nil, bundle: nil)

        self.title = "Chat"
        self.tabBarItem.image = UIImage(named: "Chat")
        self.tabBarItem.title = "Chats"
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        v = ChatOverview(
            withName: "Mariano Rajoy",
            andImage: UIImage(named: "Rajoy")!
        )
        self.view.addSubview(v)

        NSLayoutConstraint.activate([
            v.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
            v.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
            v.widthAnchor.constraint(equalToConstant: 200),
            v.heightAnchor.constraint(equalToConstant: 200)
        ])
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        self.v.applyCircleMask()
    }

}

以及 ChatOverview 类的定义:

import UIKit

class ChatOverview: UIView {

    var image: UIImage!
    var imageView: UIImageView!

    init (withName name: String, andImage image: UIImage) {
        super.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))

        self.image = image

        self.isOpaque = false
        self.translatesAutoresizingMaskIntoConstraints = false

        self.imageView = UIImageView(image: self.image)
        self.imageView.contentMode = .scaleAspectFit
        self.imageView.translatesAutoresizingMaskIntoConstraints = false
        self.addSubview(imageView)

        NSLayoutConstraint.activate([
            self.imageView.centerXAnchor.constraint(equalTo: self.centerXAnchor),
            self.imageView.centerYAnchor.constraint(equalTo: self.centerYAnchor),
            self.imageView.widthAnchor.constraint(equalTo: self.widthAnchor),
            self.imageView.heightAnchor.constraint(equalTo: self.heightAnchor)
        ])
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    func applyCircleMask () {
        self.imageView.layer.cornerRadius = self.imageView.frame.size.width / 2.0
        self.imageView.clipsToBounds = true
    }

}

【问题讨论】:

  • 你能在self.v.applyCircleMask()行执行之前打印ChatOverview的帧吗?
  • 我编写了打印代码,结果给出了宽度和高度:Frame(1463.0, 1463.0) 和 Layer(1463.0, 1463.0)。所以 imageView 的 frame 和 layer 的大小都是一样的。
  • 尝试在viewDidLoad方法的最后添加view.layoutIfNeeded()
  • 我试过了,但是不行:$

标签: ios swift uiimageview uiimage calayer


【解决方案1】:

代替 clipsToBounds,使用:

self.imageView.layer.masksToBounds = true

【讨论】:

  • .layer 从您的代码中丢失。去掉这两条线后你能看到图像吗?
  • 我已经编译了它,但没有工作,所以我没有编辑我的代码。抱歉,这一定不是解决方案。
猜你喜欢
  • 2014-07-20
  • 1970-01-01
  • 2014-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多