Ios 如何将渐变颜色设置为UIImageView边框

Ios 如何将渐变颜色设置为UIImageView边框,ios,iphone,uiimageview,gradient,Ios,Iphone,Uiimageview,Gradient,如何将渐变颜色应用于UIImageView边框颜色。我试过的是 let gradient: CAGradientLayer = CAGradientLayer() let color0 = UIColor(red:0.0/255, green:0.0/255, blue:0.0/255, alpha:0.0).CGColor let color1 = UIColor(red:0.0/255, green:0.0/255, blue: 0.0/255, alpha:0.71).C

如何将渐变颜色应用于UIImageView边框颜色。我试过的是

let gradient: CAGradientLayer = CAGradientLayer()

    let color0 = UIColor(red:0.0/255, green:0.0/255, blue:0.0/255, alpha:0.0).CGColor
    let color1 = UIColor(red:0.0/255, green:0.0/255, blue: 0.0/255, alpha:0.71).CGColor

    gradient.colors = [color0, color1]
    gradient.locations = [0.0 , 1.0]
    gradient.frame =  CGRect(x: 0.0, y: 80.0, width: self.imgProfile.frame.size.width, height: self.imgProfile.frame.size.height-35)

    self.imgProfile.layer.insertSublayer(gradient, atIndex: 0)
这是必需的

请导游,谢谢

更新:

这样做

let gradient = CAGradientLayer()
    gradient.frame =  CGRect(origin: CGPointZero, size: self.imgPeopleProfile.frame.size)
    gradient.colors = [UIColor.blueColor().CGColor, UIColor.greenColor().CGColor]

    let shape = CAShapeLayer()
    shape.lineWidth = 2
    shape.path = UIBezierPath(rect: self.imgPeopleProfile.bounds).CGPath
    shape.strokeColor = UIColor.blackColor().CGColor
    shape.fillColor = UIColor.clearColor().CGColor
    gradient.mask = shape

    imgPeopleProfile.layer.cornerRadius = imgPeopleProfile.frame.size.width / 2
    imgPeopleProfile.clipsToBounds = true
    self.imgPeopleProfile.layer.addSublayer(gradient)
它给


请拍摄图像查看大小相等的宽度和高度,例如宽度=100和高度=100,以便显示正确的圆圈。如果您想更改尺寸,请在下面的行中更改

shape.path = UIBezierPath(arcCenter: CGPoint(x: 50, y: 50), radius: CGFloat(50), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true).CGPath
上例中的圆半径为50

 let gradient = CAGradientLayer()
            gradient.frame =  CGRect(origin: CGPointZero, size: self.imageView.frame.size)
            gradient.colors = [UIColor(red:87.0/255, green:206.0/255, blue: 172.0/255, alpha:0.71).CGColor,UIColor(red:44.0/255, green:192.0/255, blue:208.0/255, alpha:1.0).CGColor]

            imageView.layer.cornerRadius = imageView.frame.size.width / 2
            imageView.clipsToBounds = true

            let shape = CAShapeLayer()
            shape.lineWidth = 5
            shape.path = UIBezierPath(arcCenter: CGPoint(x: 50, y: 50), radius: CGFloat(50), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true).CGPath
            shape.strokeColor = UIColor.blackColor().CGColor
            shape.fillColor = UIColor.clearColor().CGColor
            gradient.mask = shape

            self.imageView.layer.addSublayer(gradient)

你的方法看起来很好,你能告诉我们你得到了什么结果吗?gAlready实现了这一点,它不会给你圆角,如果你使圆角变圆,渐变颜色部分会被切掉。不精确,不生成如上所示。请检查更新如果您能告诉我您的imageview框架,请将您的imageview设置为尺寸宽度=100,高度=100,然后它将以正确的方式显示。Oks..使该imageview尺寸等于宽度和高度,您将得到正确的响应。