Swift - 蒙版图像内边框
Swift - Border Inside Masked Image
我有一个 UIBezierPath()
使用的蒙版图像,我想在蒙版部分而不是原始图像周围添加边框。在我的(无数次)尝试中,我已经通过反复试验和不知疲倦地放弃了。有更简单的方法吗?
这是我实现目标的程度:
我希望边界向内! :-(
Swift2码:
let picture = UIImageView(image: UIImage(named: "myPicture.png"))
picture.layer.contentMode = .ScaleAspectFit
picture.layer.clipsToBounds = true
let maskLayer = CAShapeLayer()
picture.layer.mask = maskLayer
maskLayer.frame = CGRect(origin: CGPointZero, size: picture.image.size)
let radius = picture.size.width/2
let center = CGPoint(x: picture.size.width/2, y: picture.size.height/2)
let path = UIBezierPath()
path.moveToPoint(center)
path.addArcWithCenter(center, radius: radius, startAngle: 0, endAngle: CGFloat(M_PI*2.0*0.2), clockwise: true)
maskLayer.path = path.CGPath
picture.layer.mask = maskLayer
picture.layer.borderWidth = 3
picture.layer.borderColor = UIColor.purpleColor().CGColor
picture.layer.cornerRadius = picture.layer.bounds.width/2
假设我理解您所说的 "I'd love to have the border go inward" 的意思 - 我建议使用另一个 CAShapeLayer
将您的边框显示为描边路径,并将其作为子图层添加到图像视图的图层中。
您只需要在您为面具定义的路径中设置您的通行证,设置您的 strokeColor
和 lineWidth
,并将 fillColor
设置为 clearColor
。
像这样应该可以解决问题:
// insert your image here
guard let img = UIImage(named: "4.png") else {
return
}
// frame of your image view
let frame = CGRectInset(view.bounds, 10, 10)
// your picture view
let pictureView = UIImageView(image: img)
pictureView.frame = frame
pictureView.contentMode = .ScaleAspectFit
pictureView.clipsToBounds = true
// the radius and center of your path
let radius = pictureView.frame.size.width*0.5
let center = CGPoint(x: pictureView.frame.size.width*0.5, y: pictureView.frame.size.height*0.5)
// your masking & stroking path
let path = UIBezierPath()
path.moveToPoint(center)
path.addArcWithCenter(center, radius: radius, startAngle: 0, endAngle: CGFloat(M_PI*2.0*0.8), clockwise: true)
path.closePath()
// the layer used to mask the image view
let maskLayer = CAShapeLayer()
maskLayer.frame = pictureView.bounds
maskLayer.path = path.CGPath
pictureView.layer.mask = maskLayer
// the layer used to draw the border
let strokeLayer = CAShapeLayer()
strokeLayer.frame = pictureView.bounds
strokeLayer.fillColor = UIColor.clearColor().CGColor
strokeLayer.path = path.CGPath
strokeLayer.strokeColor = UIColor.purpleColor().CGColor
strokeLayer.lineWidth = 10
pictureView.layer.addSublayer(strokeLayer)
view.addSubview(pictureView)
给出以下输出:
此外,我会注意到您在 OP 中提供的代码甚至没有编译 - 请始终从 IDE 复制并粘贴您的代码,而不是尝试自己输入!
我有一个 UIBezierPath()
使用的蒙版图像,我想在蒙版部分而不是原始图像周围添加边框。在我的(无数次)尝试中,我已经通过反复试验和不知疲倦地放弃了。有更简单的方法吗?
这是我实现目标的程度:
我希望边界向内! :-(
Swift2码:
let picture = UIImageView(image: UIImage(named: "myPicture.png"))
picture.layer.contentMode = .ScaleAspectFit
picture.layer.clipsToBounds = true
let maskLayer = CAShapeLayer()
picture.layer.mask = maskLayer
maskLayer.frame = CGRect(origin: CGPointZero, size: picture.image.size)
let radius = picture.size.width/2
let center = CGPoint(x: picture.size.width/2, y: picture.size.height/2)
let path = UIBezierPath()
path.moveToPoint(center)
path.addArcWithCenter(center, radius: radius, startAngle: 0, endAngle: CGFloat(M_PI*2.0*0.2), clockwise: true)
maskLayer.path = path.CGPath
picture.layer.mask = maskLayer
picture.layer.borderWidth = 3
picture.layer.borderColor = UIColor.purpleColor().CGColor
picture.layer.cornerRadius = picture.layer.bounds.width/2
假设我理解您所说的 "I'd love to have the border go inward" 的意思 - 我建议使用另一个 CAShapeLayer
将您的边框显示为描边路径,并将其作为子图层添加到图像视图的图层中。
您只需要在您为面具定义的路径中设置您的通行证,设置您的 strokeColor
和 lineWidth
,并将 fillColor
设置为 clearColor
。
像这样应该可以解决问题:
// insert your image here
guard let img = UIImage(named: "4.png") else {
return
}
// frame of your image view
let frame = CGRectInset(view.bounds, 10, 10)
// your picture view
let pictureView = UIImageView(image: img)
pictureView.frame = frame
pictureView.contentMode = .ScaleAspectFit
pictureView.clipsToBounds = true
// the radius and center of your path
let radius = pictureView.frame.size.width*0.5
let center = CGPoint(x: pictureView.frame.size.width*0.5, y: pictureView.frame.size.height*0.5)
// your masking & stroking path
let path = UIBezierPath()
path.moveToPoint(center)
path.addArcWithCenter(center, radius: radius, startAngle: 0, endAngle: CGFloat(M_PI*2.0*0.8), clockwise: true)
path.closePath()
// the layer used to mask the image view
let maskLayer = CAShapeLayer()
maskLayer.frame = pictureView.bounds
maskLayer.path = path.CGPath
pictureView.layer.mask = maskLayer
// the layer used to draw the border
let strokeLayer = CAShapeLayer()
strokeLayer.frame = pictureView.bounds
strokeLayer.fillColor = UIColor.clearColor().CGColor
strokeLayer.path = path.CGPath
strokeLayer.strokeColor = UIColor.purpleColor().CGColor
strokeLayer.lineWidth = 10
pictureView.layer.addSublayer(strokeLayer)
view.addSubview(pictureView)
给出以下输出:
此外,我会注意到您在 OP 中提供的代码甚至没有编译 - 请始终从 IDE 复制并粘贴您的代码,而不是尝试自己输入!