ios – Swift:在为UIImageView添加边框时获取小条带

当我为UI
ImageView添加边框时,我会在下面的图片上看到小黑条.我在真正的iPhone上试用我的应用程序,我也可以看到小条带.我怎样才能删除这个问题或者问题出在哪里?

有人有想法吗?

《ios – Swift:在为UIImageView添加边框时获取小条带》

这是我的UIImageView代码:

//Setup ProfileImageView
    profileImageView.layer.cornerRadius = profileImageView.frame.size.width / 2
    profileImageView.clipsToBounds = true
    profileImageView.layer.borderColor = UIColor.white.cgColor
    profileImageView.layer.borderWidth = 4.0
    profileImageView.layer.shadowOpacity = 0.12
    profileImageView.layer.shadowOffset = CGSize(width: 0, height: 2)
    profileImageView.layer.shadowRadius = 2
    profileImageView.layer.shadowColor = UIColor.black.cgColor

和我的故事板设置:

《ios – Swift:在为UIImageView添加边框时获取小条带》

我使用Xcode 10 beta 6 ……这可能是个问题吗?

最佳答案 当您设置cornerRadius时,看起来图像正在从图层的边框中流失.看这篇文章:
iOS: Rounded rectangle with border bleeds color

@FelixLam在上述帖子中提出的CAShapeLayer解决方案可能是这样的:

let lineWidth: CGFloat = 4.0 // the width of the white border that you want to set
let imageWidth = self.profileImageView.bounds.width
let imageHeight = self.profileImageView.bounds.height

// make sure width and height are same before drawing a circle
if (imageWidth == imageHeight) {
    let side = imageWidth - lineWidth
    let circularPath = UIBezierPath.init(ovalIn: CGRect(lineWidth / 2, lineWidth / 2, side, side))

    // add a new layer for the white border
    let borderLayer = CAShapeLayer()
    borderLayer.path = circularPath.CGPath
    borderLayer.lineWidth = lineWidth
    borderLayer.strokeColor = UIColor.white.cgColor
    borderLayer.fillColor = UIColor.clear.cgColor
    self.profileImageView.layer.insertSublayer(borderLayer, at: 0)

    // set the circle mask to your profile image view
    let circularMask = CAShapeLayer()
    circularMask.path = circularPath.CGPath
    self.profileImageView.layer.mask = circularMask
}
点赞