How to set corner Radius to UIImage not UIImageView in iOS Swift
Here is an extension for UIImage to set corner radius for it, not dealing with UIImageView.
extension UIImage {
// image with rounded corners
public func withRoundedCorners(radius: CGFloat? = nil) -> UIImage? {
let maxRadius = min(size.width, size.height) / 2
let cornerRadius: CGFloat
if let radius = radius, radius > 0 && radius <= maxRadius {
cornerRadius = radius
} else {
cornerRadius = maxRadius
}
UIGraphicsBeginImageContextWithOptions(size, false, scale)
let rect = CGRect(origin: .zero, size: size)
UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius).addClip()
draw(in: rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
Usage:
yourUIImage.withRoundedCorners(radius: 10)
You can try AspectFill mode, instead of AspectFit. Using AspectFill, image will fill imageView completely, and there'll be rounded corners.