13

Can any one help me...! I just want to set a corner radius to an image and also the image has to be fit in AspectFit scale.

If I give scaleToFill mode I can use the below code and its working fine. but the image is been Stretched.

self.productImg.layer.cornerRadius = 7
self.productImg.clipsToBounds = true

But when giving the scale to AspectFit it is showing as shown in below image.

The green color showing is the image View, and within that the image is setting to aspectFit.

The Actual image

Actual image

The Image when giving the aspectFit mode

Image when giving the aspectFit mode

i need the image as it is actually given and also it has to be with corner radius. So please any one give me solution to Resolve this.

Thanks in Advance...!

Frank Pavageau
  • 11,477
  • 1
  • 43
  • 53
Abirami Bala
  • 760
  • 1
  • 10
  • 25

3 Answers3

28

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)
yoAlex5
  • 29,217
  • 8
  • 193
  • 205
astghik
  • 296
  • 3
  • 4
2

You can try AspectFill mode, instead of AspectFit. Using AspectFill, image will fill imageView completely, and there'll be rounded corners.

Anton Novoselov
  • 769
  • 2
  • 7
  • 19
  • Yes, I've tried it. but with this the image is getting cropped. (The full image is not showing) – Abirami Bala Jul 29 '16 at 11:02
  • 1
    In this case, if it's necessary to show complete image, without cutting any little piece of it, you should set proper constraints for your imageView in storyboard. Check aspect ratio of your image (in Photoshop eg), and set constraint "Aspect Ratio" for your imageView to the same value. – Anton Novoselov Jul 29 '16 at 11:17
  • I've Tried it out. Same problem persists. – Abirami Bala Jul 29 '16 at 13:01
-2

Similar to Anton's answer, but with a small twist:

Place the UIImageView in a generic UIView, which acts as a container whose corner radius you can control.

Example:

let containerView = UIView()
containerView.layer.cornerRadius = 15
containerView.layer.masksToBounds = true //maybe not necessary
//set some width and height in a CGRect()

let imageView = UIImageView()
containerView.addSubview(imageView)
imageView.frame = containerView.bounds
imageView.contentMode = .scaleAspectFill
Tim Isenman
  • 147
  • 10