Swift 3:获取 UIImage 中像素的颜色(更好:UIImageView) [英] Swift 3: get color of pixel in UIImage (better: UIImageView)

查看:28
本文介绍了Swift 3:获取 UIImage 中像素的颜色(更好:UIImageView)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了不同的解决方案(例如

给我 === Optional((51, 76, 184, 255)) 在 iPhone 7 模拟器上运行时,这是不正确的...

解决方案

我写的这是一个游乐场.我用指针索引图像数据并获取 rgba 值:

func pixel(in image: UIImage, at point: CGPoint) ->(UInt8,UInt8,UInt8,UInt8)?{让宽度 = Int(image.size.width)让高度 = Int(image.size.height)让 x = Int(point.x)让 y = Int(point.y)守卫x<宽度&&y<高度其他{返回零}守卫让 cfData:CFData = image.cgImage?.dataProvider?.data, let pointer = CFDataGetBytePtr(cfData) else {返回零}让 bytesPerPixel = 4让偏移量 = (x + y * 宽度) * bytesPerPixel返回(指针[偏移],指针[偏移+ 1],指针[偏移+ 2],指针[偏移+ 3])}let image = UIImage(named: "t.png")!if let (r,g,b,a) = pixel(in: image, at: CGPoint(x: 1, y:2)) {打印(红色:\(r),绿色:\(g),蓝色:\(b),阿尔法:\(a)")}

请注意,如果您在作为 UIImageView 属性的 UIImage 上使用它,则像素坐标是原始分辨率下实际图像的像素坐标,而不是缩放后的 UIImageView 的屏幕坐标.它还尝试使用 RGB Jpg 和 RGBA PNG,并且两者都被导入为 32 位 RGBA 图像,因此它适用于两者.

I tried different solutions (e.g. this one), but the color I get back looks a bit different than in the real image. I guess it's because the image is only RGB, not RGBA. May that be an issue?

Related issue: if the UIImage has contentMode = .scaleAspectFill, do I have to do a recalculation of the image or can I just use imageView.image?

EDIT:

I tried with this extension:

extension CALayer {
    func getPixelColor(point: CGPoint) -> CGColor {
        var pixel: [CUnsignedChar] = [0, 0, 0, 0]

        let colorSpace = CGColorSpaceCreateDeviceRGB()
        let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)

        let context = CGContext(data: &pixel, width: 1, height: 1, bitsPerComponent: 8, bytesPerRow: 4, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)

        context!.translateBy(x: -point.x, y: -point.y)

        self.render(in: context!)

        let red: CGFloat   = CGFloat(pixel[0]) / 255.0
        let green: CGFloat = CGFloat(pixel[1]) / 255.0
        let blue: CGFloat  = CGFloat(pixel[2]) / 255.0
        let alpha: CGFloat = CGFloat(pixel[3]) / 255.0


        let color = UIColor(red:red, green: green, blue:blue, alpha:alpha)

        return color.cgColor
    }
}

but for some images it seems as if the coordinate system is turned around, for others I get really wrong values... what am I missing here?

EDIT 2:

I try with these images:

https://dl.dropboxusercontent.com/u/119600/gradient.png https://dl.dropboxusercontent.com/u/119600/gradient@2x.png

but I do get wrong values. They are embedded in a UIImageView but I convert the coordinates:

private func convertScreenPointToImage(point: CGPoint) -> CGPoint {
    let widthMultiplier = gradientImage.size.width / UIScreen.main.bounds.width
    let heightMultiplier = gradientImage.size.height / UIScreen.main.bounds.height

    return CGPoint(x: point.x * widthMultiplier, y: point.y * heightMultiplier)
}

This one

gives me === Optional((51, 76, 184, 255)) when running on the iPhone 7 simulator, which is not correct...

解决方案

I wrote this is a playground. I index into the image data with a pointer and grab the rgba values:

func pixel(in image: UIImage, at point: CGPoint) -> (UInt8, UInt8, UInt8, UInt8)? {
    let width = Int(image.size.width)
    let height = Int(image.size.height)
    let x = Int(point.x)
    let y = Int(point.y)
    guard x < width && y < height else {
        return nil
    }
    guard let cfData:CFData = image.cgImage?.dataProvider?.data, let pointer = CFDataGetBytePtr(cfData) else {
        return nil
    }
    let bytesPerPixel = 4
    let offset = (x + y * width) * bytesPerPixel
    return (pointer[offset], pointer[offset + 1], pointer[offset + 2], pointer[offset + 3])
}

let image = UIImage(named: "t.png")!
if let (r,g,b,a) = pixel(in: image, at: CGPoint(x: 1, y:2)) {
    print ("Red: \(r), Green: \(g), Blue: \(b), Alpha: \(a)")
}

Note that if you use this on a UIImage that is a property of a UIImageView the pixel coordinates are those of the actual image in its original resolution, not the screen coordinates of the scaled UIImageView. Also it tried with RGB Jpg and RGBA PNG and the both get imported as 32 bit RGBA images so it works for both.

这篇关于Swift 3:获取 UIImage 中像素的颜色(更好:UIImageView)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆