如何在Swift中向UIImageView添加自定义形状?

问题描述 投票:1回答:1

我正在尝试向imageView添加自定义形状。请检查以下图像。

这是必需的:

Required Image Shape

这是我到目前为止所做的:

Current Image Shape

Core Graphics我是新手,到目前为止,我已经做到了:

    private func customImageClipper(imageV: UIImageView){

    let path = UIBezierPath()

    let size = imageV.frame.size

    print(size)

    path.move(to: CGPoint(x: 0.0, y: size.height))

    path.addLine(to: CGPoint(x: 0.8, y: size.height/2))

    path.close()

    let shape = CAShapeLayer()

    shape.path = path.cgPath

    imageV.layer.sublayers = [shape]

}

我正在创建一个函数来实现这样的形状,但是每当我将imageView传递给该函数时,我根本看不到任何变化。我知道我必须从一个点移动到另一个点才能实现这种形状,但是我从未做到过。任何帮助,将不胜感激。这就是我调用此函数的方式:

imageV.layoutIfNeeded()

customImageClipper(imageV: imageV)

P.S .:我没有使用Storyboard,而是以编程方式创建的。

ios swift core-graphics uibezierpath cashapelayer
1个回答
0
投票

只需告诉您的设计师为您提供该形状的图像,您的任务将如下所示:1.用蒙版图像(即设计师图像)蒙版您的图像视图2.将您的图像添加到蒙版图像视图中。

关注此链接

© www.soinside.com 2019 - 2024. All rights reserved.