swift 获取UI上某点点颜色

Posted brave-sailor

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 获取UI上某点点颜色相关的知识,希望对你有一定的参考价值。

extension UIView
{
    func colorOfPoint (point: CGPoint) -> UIColor
    {
        var pixel = UnsafePointer<CUnsignedChar>.alloc(4)
        let colorSpace = CGColorSpaceCreateDeviceRGB()
        let bitmapInfo = CGBitmapInfo.fromRaw(CGImageAlphaInfo.PremultipliedLast.toRaw())!
        let context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, bitmapInfo)

        CGContextTranslateCTM(context, -point.x, -point.y)

        self.layer.renderInContext(context)

        CGContextRelease(context)
        CGColorSpaceRelease(colorSpace)

        return UIColor(red: Float(pixel [0]) / 255.0, green: Float (pixel [1]) / 255.0, blue: Float (pixel [2]) / 255.0 , alpha: Float (pixel [3]) / 255.0)
    }
}

以上是关于swift 获取UI上某点点颜色的主要内容,如果未能解决你的问题,请参考以下文章