1. 程式人生 > >ios 圖片圓角設定

ios 圖片圓角設定

   對於圓角的設定,很簡單,只需要layer的兩個屬性即可,就想下面一樣

view.layer.cornerRadius = 5;
view.layer.masksToBounds = true;
這樣就可以設定圓角了.很簡單.
但是,如果在一個列表裡要顯示多個圖片,並且還需要圓角的設定,那麼上邊的就不太好了,你會發現嚴重影響,使用者體驗.那麼解決方法是什麼呢,下面直接上程式碼.
<pre name="code" class="html">func drawRectWithRroundedCorner(randius : CGFloat,sizeFit : CGSize) -> UIImage {
        let rect = CGRect(origin: CGPoint.zero, size: sizeFit);
        UIGraphicsBeginImageContextWithOptions(rect.size, false, UIScreen.mainScreen().scale);
        let context = UIGraphicsGetCurrentContext()
        let path = UIBezierPath(roundedRect: rect, byRoundingCorners: UIRectCorner.AllCorners, cornerRadii: CGSize(width: randius, height: randius));
        CGContextAddPath(context, path.CGPath);
        CGContextClip(context);
        self.drawInRect(rect);
        CGContextDrawPath(context, CGPathDrawingMode.FillStroke);
        let outImg = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return outImg;
    }

這個就是設定圖片圓角的方法,不是view,而是圖片,用它新增的imageView上就可以顯示出來.
其實也可以設定view為圓角,通過CAShapeLayer具體怎麼做可以參考我以後的文章了