iOS Find Color at Point Between Two Colors

后端 未结 5 794
感情败类
感情败类 2020-12-05 06:00

I have a problem: I need to be able to take two colors and make a \'virtual gradient\' out of them. I then need to be able to find the color at any point on this line. My cu

5条回答
  •  悲&欢浪女
    2020-12-05 06:19

    I would like to extend/modify @Sebastien Windal´s answer, as his answer works great for my use case, but can be further improved by getting the pixelData directly from the context (see Get pixel data as array from UIImage/CGImage in swift).

    I implemented his answer in swift, therefore the changes are also written in swift.

    Just pass the Int Array to the context before drawing the gradient. The array will then be filled with the pixelData when drawing the context.

    let dataSize = tmpImagewidth * 1 * 4
    var pixelData = [UInt8](repeating: 0, count: Int(dataSize))
    
    
    let context = CGContext(data: &pixelData, width: Int(tmpImagewidth), height: 1, bitsPerComponent: 8, bytesPerRow: 4 * Int(tmpImagewidth), space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)
    

    Then we can skip creating an image first to read the pixelData from there.

提交回复
热议问题