2016-01-25 86 views
6

所以我想在地圖上顯示一些圖片作爲註釋。爲了做到這一點,我需要添加MKAnnotationView的圖像屬性。我正在使用常規圖像,但我希望它們是圓形的並帶有邊框。所以我找到了一種繞UIImage的方法,我找到了爲UIImage添加邊框的方法,但邊框似乎並沒有添加(我實際上並沒有在屏幕上顯示圖像,也許這就是問題所在)。舍入UIImage並添加邊框

我用這個答案https://stackoverflow.com/a/29047372/4665643對邊界稍作修改。即:

imageView.layer.borderWidth = 1.5 
imageView.layer.borderColor = UIColor.whiteColor().CGColor 
imageView.clipsToBounds = true 

但我在地圖上的圖像沒有任何邊框。有什麼建議麼 ?

+0

它應該是'.maskToBounds'不'clipToBounds'? – Tj3n

+0

嘿,所以實際上最初的答案也有這個,但它似乎仍然不起作用。 –

+0

嗯...那麼我認爲有一個外部的看法,扭曲這個圖像的大小?,外部視圖應該更大或實際顯示邊框。我試過你的代碼,它工作完美,增加了一個邊框 – Tj3n

回答

5
imageView.layer.masksToBounds=true 
imageView.layer.borderWidth=1.5 
imageView.layer.borderColor = UIColor.whiteColor().CGColor 
imageView.layer.cornerRadius=imageView.bounds.width/2 

試試這個。

+1

是imageView.layer.borderColor = UIColor.white.cgColor – Maziyar

+0

@Maziyar,在Swift 4.0中,它是:imageView.layer.borderColor = .white.cgColor –

0

只是修復它。顯然,一切都很完美,但我沒有看到邊界。原始圖像大約爲300x300像素,並且具有1.5像素邊框,我將其裁剪爲適合40x40的邊框,因此邊框幾乎不顯眼。將邊框寬度更改爲更大的數字使其可見。

+0

如果用iphone 6 plus在模擬器中測試,我會注意到很多的小行(如tableview的分隔符)也會消失 – Tj3n

1

Supportive image added

我設置masksToBounds,和它的工作。

layer.masksToBounds = true 
2

使用該擴展的UIImageView:

func cropAsCircleWithBorder(borderColor : UIColor, strokeWidth: CGFloat) 
{ 
    var radius = min(self.bounds.width, self.bounds.height) 
    var drawingRect : CGRect = self.bounds 
    drawingRect.size.width = radius 
    drawingRect.origin.x = (self.bounds.size.width - radius)/2 
    drawingRect.size.height = radius 
    drawingRect.origin.y = (self.bounds.size.height - radius)/2 

    radius /= 2 

    var path = UIBezierPath(roundedRect: CGRectInset(drawingRect, strokeWidth/2, strokeWidth/2), cornerRadius: radius) 
    let border = CAShapeLayer() 
    border.fillColor = UIColor.clearColor().CGColor 
    border.path = path.CGPath 
    border.strokeColor = borderColor.CGColor 
    border.lineWidth = strokeWidth 
    self.layer.addSublayer(border) 

    path = UIBezierPath(roundedRect: drawingRect, cornerRadius: radius) 
    let mask = CAShapeLayer() 
    mask.path = path.CGPath 
    self.layer.mask = mask 
} 

用法:

 self.circleView.cropAsCircleWithBorder(UIColor.redColor(), strokeWidth: 20) 

結果:

Result

1

簡單的一行代碼,其作品對M Ë

self.profileImage.layer.cornerRadius = self.profileImage.frame.size.width/2 
2

用於與邊框圓角的圖像,你可以做到這一點從用戶自定義運行時也屬性,無需編寫代碼爲。

請檢查下面的圖片用於設置

enter image description here

而且在你的代碼,改變

imageView.layer.clipsToBounds = true 

此,

imageView.layer.masksToBounds = true 
+0

這對我不起作用,而UIImageView沒有masksToBounds屬性。 – Nestor

+0

@Nestor,你可以使用'imageView.layer.masksToBounds = true' – Rajat

4

如果你想添加您需要確保您添加一些額外空間的圖片邊框否則你的邊框將被放置在你的圖像的頂部。解決方法是將您筆畫的寬度加倍到圖像的寬度和高度,然後將contentMode更改爲.Center

extension UIImage { 
    func roundedWithBorder(width: CGFloat, color: UIColor) -> UIImage? { 
     let square = CGSize(width: min(size.width, size.height) + width * 2, height: min(size.width, size.height) + width * 2) 
     let imageView = UIImageView(frame: CGRect(origin: .zero, size: square)) 
     imageView.contentMode = .center 
     imageView.image = self 
     imageView.layer.cornerRadius = square.width/2 
     imageView.layer.masksToBounds = true 
     imageView.layer.borderWidth = width 
     imageView.layer.borderColor = color.cgColor 
     UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, false, scale) 
     defer { UIGraphicsEndImageContext() } 
     guard let context = UIGraphicsGetCurrentContext() else { return nil } 
     imageView.layer.render(in: context) 
     return UIGraphicsGetImageFromCurrentImageContext() 
    } 
} 

遊樂場測試:在斯威夫特3

let profilePicture = UIImage(data: try! Data(contentsOf: URL(string:"http://i.stack.imgur.com/Xs4RX.jpg")!))! 

profilePicture.roundedWithBorder(width: 100, color: .red) 
1

利奧Dabus的解決方案:

extension UIImage { 
    func roundedImageWithBorder(width: CGFloat, color: UIColor) -> UIImage? { 
     let square = CGSize(width: min(size.width, size.height) + width * 2, height: min(size.width, size.height) + width * 2) 
     let imageView = UIImageView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: square)) 
     imageView.contentMode = .center 
     imageView.image = self 
     imageView.layer.cornerRadius = square.width/2 
     imageView.layer.masksToBounds = true 
     imageView.layer.borderWidth = width 
     imageView.layer.borderColor = color.cgColor 
     UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, false, scale) 
     guard let context = UIGraphicsGetCurrentContext() else { return nil } 
     imageView.layer.render(in: context) 
     let result = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return result 
    } 
}