IOS: create a UIImage or UIImageView with rounded corners

后端 未结 12 740
执笔经年
执笔经年 2020-12-07 07:26

Is it possible create an UIImage or an UIImageView with rounded corners? Because I want take an UIImage and show it inside an UI

相关标签:
12条回答
  • 2020-12-07 07:52

    Yes, it is possible.
    Import the QuartzCore (#import <QuartzCore/QuartzCore.h>) header and play with the layer property of the UIImageView.

    yourImageView.layer.cornerRadius = yourRadius;
    yourImageView.clipsToBounds = YES;
    

    See the CALayer class reference for more info.

    0 讨论(0)
  • 2020-12-07 07:53
    # import QuartzCore framework
    imageView.layer.cornerRadius=imgvwUser.frame.size.width/2;
    imageView.layer.masksToBounds = YES;
    

    The height and the width of the imageView must be same to get rounded corners.

    0 讨论(0)
  • 2020-12-07 07:58
    uiimageview.layer.cornerRadius = uiimageview.frame.size.height/2;
    uiimageview.clipToBounds = YES;
    
    #import <QuartzCore/QuartzCore.h>
    
    0 讨论(0)
  • 2020-12-07 07:58

    Setting cornerRadius and clipsToBounds is the right way to do this. However if the view's size changes, the radius will not update. In order to get proper resizing and animation behavior, you need to create a UIImageView subclass.

    class RoundImageView: UIImageView {
        override var bounds: CGRect {
            get {
                return super.bounds
            }
            set {
                super.bounds = newValue
                setNeedsLayout()
            }
        }
    
        override func layoutSubviews() {
            super.layoutSubviews()
            layer.cornerRadius = bounds.width / 2.0
            clipsToBounds = true
        }
    }
    
    0 讨论(0)
  • 2020-12-07 07:58

    Try this to get rounded corners of the image View and also to colour the corners:

    imageView.layer.cornerRadius = imageView.frame.size.height/2;
    imageView.layer.masksToBounds = YES;
    imageView.layer.borderColor = [UIColor colorWithRed:148/255. green:79/255. blue:216/255. alpha:1.0].CGColor;
    imageView.layer.borderWidth=2;
    

    Condition*: The height and the width of the imageView must be same to get rounded corners.

    0 讨论(0)
  • 2020-12-07 08:02

    Circle with UIBeizerPath #Swift-3 && #imageExtension

    class ViewController: UIViewController {
        @IBOutlet weak var imageOutlet: UIImageView!
        override func viewDidLoad() {
            super.viewDidLoad()
            let image = UIImage(named: "IMG_0001.JPG")
            if let image = image {
                let renderimage = image.imageCroppingBezierPath(path: UIBezierPath(arcCenter: CGPoint(x:image.size.width/2,y:image.size.width/2 )  , radius: 200, startAngle: 0, endAngle:  (2 * CGFloat(M_PI) ), clockwise: true) )
                    imageOutlet.image = renderimage
            }
        }
    }
    
    
    extension UIImage {
        func imageCroppingBezierPath(path:UIBezierPath) ->UIImage {
    
            let frame = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
    
            //Defining a graphic context  to paint on
            UIGraphicsBeginImageContextWithOptions(self.size, false, 0.0)
            //Get the current graphics context (if it exists)
            let context = UIGraphicsGetCurrentContext()
            //save the current graphic context
            context?.saveGState()
            // clipping area
            path.addClip()
            self.draw(in: frame)
    
            //To extract an image from our canvas
            let image = UIGraphicsGetImageFromCurrentImageContext()
            //restore graphic context
            context?.restoreGState()
            //remove current context from stack
            UIGraphicsEndImageContext()
            return image!
        }
    }
    
    0 讨论(0)
提交回复
热议问题