How to set corner Radius to UIImage not UIImageView in iOS Swift
Asked Answered
B

2

14

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...!

Basidium answered 29/7, 2016 at 9:34 Comment(3)
ok please refer this link if it helps to you #10564486Bedard
Tried it out. Same problem persists.Basidium
Still I didn't find a right solution for this question..! can anyone help me in this case....?Basidium
B
35

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)
Blurb answered 22/12, 2018 at 19:7 Comment(3)
Code only answers are not answers; provide explanation, please (and format correctly).Collation
what is size in line 4 ?!Spelldown
size is a property of UIImage (self.size, where self is UIImage). it is available, because we are inside extension of UIImage.Blurb
A
2

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

Aestival answered 29/7, 2016 at 10:37 Comment(4)
Yes, I've tried it. but with this the image is getting cropped. (The full image is not showing)Basidium
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.Aestival
I've Tried it out. Same problem persists.Basidium
This is not a solution but a comment. Author needs to set corner of image and not UIImageView which is a valid use case.Sungsungari

© 2022 - 2024 — McMap. All rights reserved.