2
class PPAvatarCollectionCell: UICollectionViewCell {
    var imageView:UIImageView!

override init(frame: CGRect) {
    super.init(frame: frame)

    imageView = UIImageView(frame: CGRect(origin: CGPointMake(0, 0), size: CGSizeMake(frame.size.width, frame.size.height)))

    self.addSubview(imageView)

    imageView.contentMode = .ScaleAspectFill
    imageView.backgroundColor = UIColor.whiteColor()

    imageView.layer.borderColor = UIColor.greenColor().CGColor
    imageView.layer.borderWidth = 10
    imageView.image = UIImage(named: "demo")
    imageView.layer.cornerRadius = frame.size.width*0.5
    imageView.clipsToBounds = true

}

the border is smooth and great with above code but after add imageView.image = UIImage(named: "demo")

the imageView's border is no longer smooth. Why did this happen ?

smooth image

not smooth image


UPDATE:

Seems something wrong with layer.radius ,the border is smooth even with image property set on imageView after remove imageView.layer.cornerRadius = frame.size.width*0.5

enter image description here


UPDATE 2: turn out to be something wrong with UICollectionViewCell , imageView is a part of UICollectionViewCell


PeiweiChen
  • 413
  • 5
  • 16

2 Answers2

0

u can use 2 imageview 1 to show image and second to hide and show circular image.

1st imageview show image as it is.

2nd imageview which will have white background and green circle above 1st image and middle of image will be transparent

like this

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
vaibby
  • 1,255
  • 1
  • 9
  • 23
0

Basically, your image should be a square. Than do these:

imageView.layer.cornerRadius = self.frame.height / 2
imageView.layer.masksToBounds = false
imageView.clipsToBounds = true
imageView.image = "yourSquareImage"

Check the full solution with easy using extension: https://stackoverflow.com/a/36102901/2125010

Community
  • 1
  • 1
fatihyildizhan
  • 8,614
  • 7
  • 64
  • 88