ribesg
08/20/2021, 10:11 AMCAGradientLayer
in Kotlin/Native? I really can’t get it to work even on a simple view and I feel like I checked all the problems people usually encounter when using it in ObjC/SwiftThomas
08/20/2021, 11:06 AMThomas
08/20/2021, 11:06 AMThomas
08/20/2021, 11:08 AMThomas
08/20/2021, 11:09 AMinternal actual fun createGradientImage(
colors: List<Color>,
positions: List<Float>?,
width: Int,
height: Int
): UIImage {
val layer = CAGradientLayer()
layer.type = kCAGradientLayerConic
layer.colors = colors.map { CFBridgingRelease(CFRetain(it.toUIColor().CGColor)) }
positions?.let { layer.locations = it }
layer.frame = CGRectMake(0.0, 0.0, width.toDouble(), height.toDouble())
layer.startPoint = CGPointMake(0.5, 0.5)
layer.endPoint = CGPointMake(1.0, 0.5)
return imageFromLayer(layer)
}
private fun imageFromLayer(layer: CALayer): UIImage {
layer.frame.useContents {
UIGraphicsBeginImageContextWithOptions(this.size.readValue(), false, UIScreen.mainScreen.scale)
layer.renderInContext(UIGraphicsGetCurrentContext())
val outputImage = UIGraphicsGetImageFromCurrentImageContext()!!
UIGraphicsEndImageContext()
return outputImage
}
}
ribesg
08/20/2021, 11:41 AMribesg
08/20/2021, 11:45 AMribesg
08/20/2021, 12:01 PMCAGradientLayer().apply {
colors = listOf(
CFBridgingRelease(UIColor.redColor.CGColor),
CFBridgingRelease(UIColor.greenColor.CGColor)
)
}
Now I just need to fix the usual problem of layers not being updated by autolayout, but that’s it. Thanks @ThomasThomas
08/20/2021, 12:03 PMThomas
08/20/2021, 12:03 PMSam
08/20/2021, 1:35 PM