UIImageView图层边框问题

时间:2017-06-21 08:07:24

标签: ios swift uiimageview uicollectionviewcell

我有一个UIImageView并设置了它的图层borderColorborderWidthcornerRadius

imageView.layer.cornerRadius = imageView.bounds.height/2.0
imageView.layer.borderWidth = 6
imageView.layer.borderColor = UIColor.white.cgColor
imageView.clipsToBounds = true

。在此之后,我预计边界不会完全覆盖UIImageView。 我的UIImageView尺寸为110x110,而cornerRadius为55。

enter image description here

如您所见,图像的某些部分在边框外可见。 我的UIImageView格式为.pdf格式,maskToBounds = true 所以,我不明白我做错了什么?

3 个答案:

答案 0 :(得分:1)

我正在使用下面的代码,它的工作非常好。请试试

<style>    
.dropdown-menu .sub-menu {
        left: 100%;
        position: absolute;
        top: 0;
        visibility: hidden;
        margin-top: -1px;
    }

    .dropdown-menu li:hover .sub-menu {
        visibility: visible;
    }


    .navbar .sub-menu:before {
        border-bottom: 7px solid transparent;
        border-left: none;
        border-right: 7px solid rgba(0, 0, 0, 0.2);
        border-top: 7px solid transparent;
        left: -7px;
        top: 10px;
    }

    .navbar .sub-menu:after {
        border-top: 6px solid transparent;
        border-left: none;
        border-right: 6px solid #fff;
        border-bottom: 6px solid transparent;
        left: 10px;
        top: 11px;
        left: -6px;
    }

</style>

<li class="dropdown">
  <a href="#" class="dropdown-toggle user-state:active" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Guide <span class="caret"></span></a>
  <ul class="dropdown-menu" role="menu">
     <li class="dropdown">
        <a href="#" class="dropdown-toggle" data-toggle="dropdown">Diamond <b class="caret"></b></a>
          <ul class="dropdown-menu sub-menu" role="menu">
                                            <li><a href="@Url.Action("GuideView" , "Base" , new { id = 1 })">Carat Weight</a></li>
                                            <li><a href="@Url.Action("GuideView" , "Base" , new { id = 2 })">Clarity</a></li>
          </ul>
        </li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Emerald <span class="caret"></span></a>
            <ul class="dropdown-menu sub-menu">
                                            <li><a href="@Url.Action("GuideView" , "Base" , new { id = 3 })">Clarity</a></li>
                                            <li><a href="@Url.Action("GuideView" , "Base" , new { id = 4 })">Color</a></li>
                                            <li><a href="@Url.Action("GuideView" , "Base" , new { id = 5 })">Cut</a></li>
             </ul>
           </li>
         </ul>
       </li>

答案 1 :(得分:0)

使用以下代码将您的pdf文件转换为UIImage。

func drawPDFfromURL(url: URL) -> UIImage? {
    guard let document = CGPDFDocument(url as CFURL) else { return nil }
    guard let page = document.page(at: 1) else { return nil }
let pageRect = page.getBoxRect(.mediaBox)
let renderer = UIGraphicsImageRenderer(size: pageRect.size)
let img = renderer.image { ctx in
    UIColor.white.set()
    ctx.fill(pageRect)

    ctx.cgContext.translateBy(x: 0.0, y: pageRect.size.height);
    ctx.cgContext.scaleBy(x: 1.0, y: -1.0);

    ctx.cgContext.drawPDFPage(page);
}
 return img
}

然后进入你的imageview

imageView.image = image;

然后添加边框和所有

imageView.layer.cornerRadius = 55;
imageView.clipsToBounds = YES;
imageView.borderColor = [UIColor grayColor];
imageView.borderWidth = 4;

答案 2 :(得分:-3)

设置UIImageView图层masksToBounds属性:

yourImageView.masksToBounds = YES;