UIColor和CGColor之间的不同颜色

问题描述 投票:0回答:1

我注意到UIColor和CGColor之间有奇怪的行为。

使用此代码:

let rect = UIView(frame: CGRect(x: 0, y: 0, width: 60, height: 60))
rect.backgroundColor = UIColor(named: "bgStart")
addSubview(rect)

我得到了想要的颜色并将其创建为素材资源(251,16,78 | sRGB)。

但是使用CoreGraphics,您在图片中看到的颜色略有不同。 (我使用CGGradient,因为将来将来它必须是渐变色时才能获得正确的颜色)。

let context = UIGraphicsGetCurrentContext()!
let gradient = CGGradient(colorsSpace: nil, colors: [UIColor(named: "bgStart")!.cgColor,UIColor(named: "bgStart")!.cgColor] as CFArray, locations: [0, 1])!
let rectangle = UIBezierPath(rect: rect)
context.saveGState()
rectangle.addClip()
context.drawLinearGradient(gradient, start: CGPoint(x: rect.size.width, y: 0), end: CGPoint(x: rect.size.width, y: rect.size.height), options: [])
context.restoreGState()

Here the result (1 - UIColor | 2 - CGColor)

ios swift xcode uicolor cgcolor
1个回答
0
投票
为什么不尝试其他方式:

let gradientLayer = CAGradientLayer() gradientLayer.frame = self.view.bounds gradientLayer.colors = [UIColor(named: "bgStart"), UIColor(named: "bgStart")] self.view.layer.insertSublayer(gradientLayer, at: 0)

© www.soinside.com 2019 - 2024. All rights reserved.