Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIImage到Swift 4中的Shapelayer_Ios_Swift4 - Fatal编程技术网

Ios UIImage到Swift 4中的Shapelayer

Ios UIImage到Swift 4中的Shapelayer,ios,swift4,Ios,Swift4,如何使用swift 4在iOS中用图像填充形状层?我试过使用下面的方法。我有一个黑色的背景。这里self是一个Shapelayer,我将在这里设置一个图像 let image = UIImage(named: "ACVResources.bundle/temp.png")! let imageLayer = CALayer() imageLayer.contents = UIImage(named: "image")?.cgImage // Assign your image image

如何使用swift 4在iOS中用图像填充形状层?我试过使用下面的方法。我有一个黑色的背景。这里self是一个Shapelayer,我将在这里设置一个图像

 let image = UIImage(named: "ACVResources.bundle/temp.png")!
 let imageLayer = CALayer()
 imageLayer.contents = UIImage(named: "image")?.cgImage // Assign your image
 imageLayer.frame = self.frame
 imageLayer.mask = self
 self.masksToBounds = true
 self.setNeedsDisplay()

首先,必须添加填充视图的CALayer(imageLayer),然后需要设置view.layer遮罩shapeLayer。 我举一个例子:

let customView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 500))
    customView.backgroundColor = .red
    view.addSubview(customView)

    let imageLayer = CALayer()
    let image = UIImage(named: "pxl.jpeg")
    imageLayer.contents = image?.cgImage
    imageLayer.frame = customView.frame
    customView.layer.addSublayer(imageLayer)

    let shape = CAShapeLayer()
    let path = UIBezierPath()
    path.move(to: CGPoint(x: customView.frame.size.width / 2, y: 50))
    path.addLine(to: CGPoint(x: customView.frame.size.width, y: customView.frame.size.height / 2))
    path.addLine(to: CGPoint(x: 0, y: customView.frame.size.height / 2))
    path.close()
    shape.path = path.cgPath

    customView.layer.mask = shape

这里有一个CAlayer和添加图像。对我来说,我有一台自动取款机。我需要将图像添加到该层。在这里,您将向视图中添加一个CAShapelayer和Masking我理解您的意思,但唯一可行的方法是将图像设置为CAShapelayer的compositingFilter属性,但效果不太好