1

I have a UIImageView and set it's layer's borderColor, borderWidth, and cornerRadius like

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

. After this, I expect that border not fully covering UIImageView. My UIImageView has 110x110 Size and cornerRadius of 55.

enter image description here

As you can see, some parts of the image are visible outside border. My UIImageView is in .pdf format, and it's maskToBounds = true So, I don't understand what I'm doing wrong?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Nazariy Vlizlo
  • 778
  • 7
  • 16
  • 1
    I think u need to draw rounded corner yourself instead of setting the border width, see this link might helpful https://stackoverflow.com/questions/28434313/uiimageviews-layer-border-visible-inset – Shankar BS Jun 21 '17 at 08:16
  • Kindly try `imageView.clipsToBounds = true` – ovo Jun 21 '17 at 08:32
  • That's likely due to anti-aliasing effects. Make the radius of the image 2 pixels smaller than the radius of the view with the border. – fishinear Jun 21 '17 at 14:12

3 Answers3

1

I am using below code and its working perfectly fine. Kindly try

imageView.layer.cornerRadius = imageView.frame.size.width/2;
imageView.clipsToBounds = YES;
imageView.layer.cornerRadius = 6;
Raza.najam
  • 769
  • 6
  • 15
0

Convert your pdf file to UIImage using the below code.

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
}

Then put into your imageview

imageView.image = image;

Then add your border and all

imageView.layer.cornerRadius = 55;
imageView.clipsToBounds = YES;
imageView.borderColor = [UIColor grayColor];
imageView.borderWidth = 4;
Saranjith
  • 11,242
  • 5
  • 69
  • 122
-4

Set your UIImageView layer masksToBounds property :

yourImageView.masksToBounds = YES;
NotABot
  • 516
  • 1
  • 8
  • 27