码迷,mamicode.com
首页 > 编程语言 > 详细

swift 获取UI上某点点颜色

时间:2016-10-11 16:32:18      阅读:480      评论:0      收藏:0      [点我收藏+]

标签:

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上某点点颜色

标签:

原文地址:http://www.cnblogs.com/Free-Thinker/p/5949760.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!