ios - UIImageView的图层边框可见内嵌

标签 ios uiimageview calayer

刚刚做了这个:

self.imageView.backgroundColor = color1;
self.imageView.layer.masksToBounds = YES;
self.imageView.layer.borderColor = color1;
self.imageView.layer.borderWidth = 3.0;

这是问题的屏幕截图:

Screenshot of a problem

我可以看到图像、边框和超出边框的图像片段...

enter image description here enter image description here

如何修复它?

最佳答案

我在使用图层上的边框属性时遇到了类似的问题,并通过在顶部添加 CAShapeLayer 设法解决了该问题。
我无法告诉您它在旧设备上的表现如何,因为还没有使用许多动画等对其进行测试,但我真的怀疑它会减慢它们的速度。

这是 Swift 中的一些代码:

    imageView.backgroundColor = UIColor(red: 0.1137, green: 0.2745, blue: 0.4627, alpha: 1.0)
    imageView.layer.masksToBounds = true
    imageView.layer.cornerRadius = imageView.bounds.height / 2.0

    let stroke:CAShapeLayer = CAShapeLayer()
    let rect = imageView.bounds
    let strokePath = UIBezierPath(roundedRect: rect, cornerRadius: imageView.bounds.height / 2.0)

    stroke.path = strokePath.CGPath
    stroke.fillColor = nil
    stroke.lineWidth = 3.0
    stroke.strokeColor = UIColor(red: 0.1137, green: 0.2745, blue: 0.4627, alpha: 1.0).CGColor

    stroke.frame = imageView.bounds
    imageView.layer.insertSublayer(stroke, atIndex: 1)

希望它也适用于您的情况

关于ios - UIImageView的图层边框可见内嵌,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28434313/

相关文章:

ios - 当 switch case 的默认 block 只有 id 调试行时,我的程序会在生产中崩溃吗

ios - 避免在 Objective-C 中滥用单例

ios - 使用 UIAlertViewStyleSecureTextinput 更改 UIImage

swift - 在 CALayer 中绘制 UIBezierPath 时上下文无效

ios - 在 viewDidLoad 中异步加载图像,并带有事件指示器

ios - 执行segue会抛出错误

ios - 如何将操作发送到从 UIAlertView 委托(delegate)的 UIImage subview

objective-c - 从外部源 iOS 下载图像

swift - 当 superView 的比例变化时,CABasicAnimation 绘制路径不会缩放

swift - CABasicAnimation 创建 CALayer 的空默认值副本