2015-03-03 102 views
2

所以我遵循AppCoda圍繞輪廓圖像角落的教程,除了一件事外,它運行良好。無論圖像是圓形還是圓形,圖像都有一點滲透(特別是在使用白色邊框的情況下)。iOS圓角輪廓圖像的清潔角落

enter image description here

self.imageview.image = image 
self.imageview.layer.cornerRadius = 10.0 
self.imageview.layer.borderWidth = 3.0 
self.imageview.layer.borderColor = UIColor.whiteColor().CGColor 
self.imageview.clipsToBounds = true 

enter image description here

+0

只是一個建議:你有沒有嘗試設置視圖的背景顏色爲清晰的顏色? – 2015-03-03 02:16:20

+0

剛試過,沒有效果 – milesper 2015-03-03 02:18:51

+0

試試imageview.layer.masksToBounds = YES; – 2015-03-03 02:45:34

回答

0

您可以創建在矩形的面具。這似乎給了乾淨的邊緣,至少在遊樂場。這裏是代碼,但您需要修改它以獲得四舍五入的內部矩形。

// simple red rect 
var view = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200)) 
view.backgroundColor = UIColor.redColor() 
view.layer.borderColor = UIColor.whiteColor().CGColor 
view.layer.borderWidth = 6.0 

// path for the mask 
let rectanglePath = UIBezierPath(roundedRect:view.bounds, cornerRadius: 20) 
// applying the mask over the view 
let maskLayer = CAShapeLayer() 
maskLayer.frame = view.bounds 
maskLayer.path = rectanglePath.CGPath 
view.layer.mask = maskLayer 
6

您還可以添加蒙版,其是插入一點點,如果你想:

let path = UIBezierPath(roundedRect: CGRectInset(imageView.bounds, 0.5, 0.5), cornerRadius: 10.0) 
let mask = CAShapeLayer() 
mask.path = path.CGPath 
imageview.layer.mask = mask 
-1

一個簡單的解決方案是,你可以放大層的邊界一點點地覆蓋視圖的圖像的邊緣:

CGFloat offset = 1.f; // .5f is also good enough 
self.imageview.image = image; 
self.imageview.layer.cornerRadius = 10.0; 
self.imageview.layer.borderWidth = 3.0 + offset; 
self.imageview.layer.borderColor = UIColor.whiteColor().CGColor; 
self.imageview.layer.masksToBounds = YES; 

[self.imageview.layer setBounds:CGRectMake(-offset, 
              -offset, 
              CGRectGetWidth(self.imageview.frame) + offset * 2.f, 
              CGRectGetHeight(self.imageview.frame) + offset * 2.f)];