Ios 如何从组件创建CGColorRef
我有一行代码Ios 如何从组件创建CGColorRef,ios,objective-c,Ios,Objective C,我有一行代码 CAShapeLayer *shapeLayer = [CAShapeLayer layer]; shapeLayer.strokeColor = [UIColor colorWithRed:r green:g blue:b alpha:1.f].CGColor; 我可以用我们的UIColor重写它吗?我的意思是,我可以从a、r、g、b组件创建CGColorRef,并将其传递给shapeLayer.strokeColor,考虑使用ARC。是的,您可以这样做。试试下面的 CGCol
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.strokeColor = [UIColor colorWithRed:r green:g blue:b alpha:1.f].CGColor;
我可以用我们的
UIColor
重写它吗?我的意思是,我可以从a、r、g、b组件创建CGColorRef
,并将其传递给shapeLayer.strokeColor
,考虑使用ARC。是的,您可以这样做。试试下面的
CGColorCreate(CGColorSpaceCreateDeviceRGB(), [1.0, 0.5, 0.5, 1.0]) //array values are in order- red, green, blue, alpha
请参阅如果要避免内存泄漏,应释放
cgcolorsecretf
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGColorRef aColor = CGColorCreate( colorspace, components );
shapeLayer.strokeColor = aColor;
CGColorRelease( aColor );
CGColorSpaceRelease(colorspace);
好吧,这是有道理的。我一时糊涂了:)当然,你可以使用
CGColorCreate
创建一个CGColorRef
。您只需提供一个颜色空间和颜色组件。参见.OP指定的obj-c,您使用的是swift数组。你需要一个C数组。您也希望在使用后释放颜色空间。谢谢。顺便说一句,它看起来比一行更复杂shapeLayer.strokeColor=[uicolorWithred:r green:g blue:b alpha:1.f].CGColor代码>。从性能的角度看,哪一个更好?这是事实,因为它通常被用作“[UIColor whiteColor].CGColor”:)