UIImageView's layer border visible inset

2019-04-14 01:16发布

Just did this:

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

Here is screenshot of the problem:

Screenshot of a problem

I can see image, borders, and image fragments out of the border…

enter image description here enter image description here

How can I fix it?

2条回答
爷的心禁止访问
2楼-- · 2019-04-14 01:49

I had a similar issue using the border properties on the layer and managed to solve it by adding a CAShapeLayer on top.
I can't tell you how it will behave on older devices as haven't tested it with many animations and so on, but I really doubt it will slow them down.

Here is some code in 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)

Hope it will work in your case as well

查看更多
冷血范
3楼-- · 2019-04-14 01:50

Try :

self.imageView.layer.shouldRasterize = TRUE;
查看更多
登录 后发表回答