Swift - 读取.jpg图像的像素颜色返回不正确的值

时间:2023-02-01 22:22:36

I'm trying to get the UIColor of each pixel from a jpg image in swift, the following code successfully gets each pixel color however the results are confusing me:

我试图从swift中的jpg图像中获取每个像素的UIColor,以下代码成功获取每个像素颜色,但结果令我困惑:

func renderLandscape() {

    var landscapeImage = UIImage(named: "test")

    for var xPos = 0 as CGFloat; xPos < landscapeImage?.size.width; xPos++ {
        for var yPos = 0 as CGFloat; yPos < landscapeImage?.size.height; yPos++ {
            var currentColor = landscapeImage?.getPixelColor(CGPointMake(xPos, yPos))
            NSLog("Color: %@", currentColor!)
        }
    }
}

I've created an extension of UIImage here:

我在这里创建了UIImage的扩展:

extension UIImage {
    func getPixelColor(pos: CGPoint) -> UIColor {

        var pixelData = CGDataProviderCopyData(CGImageGetDataProvider(self.CGImage))
        var data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)

        var pixelInfo: Int = ((Int(self.size.width) * Int(pos.y)) + Int(pos.x)) * 4

        //NSLog("Data: %@", data.encode())

        var r = CGFloat(data[pixelInfo]) / CGFloat(255.0)
        var g = CGFloat(data[pixelInfo+1]) / CGFloat(255.0)
        var b = CGFloat(data[pixelInfo+2]) / CGFloat(255.0)
        var a = CGFloat(data[pixelInfo+3]) / CGFloat(255.0)

        return UIColor(red: r, green: g, blue: b, alpha: a)
    }  
}

Obviously the output is extremely long however here is the first 41 lines (the first column of the image):

显然输出非常长但是这里是前41行(图像的第一列):

2015-05-12 22:21:58.793 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.793 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.794 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.794 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.794 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.794 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.794 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.794 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.794 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.794 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.795 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.795 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.795 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.797 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.798 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.798 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.798 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.798 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.798 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.799 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.799 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.799 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.799 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.799 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.814 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.814 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.814 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.814 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.814 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.814 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.815 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.815 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.815 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0 0 1
2015-05-12 22:21:58.815 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0.00392157 0 1
2015-05-12 22:21:58.815 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0.00392157 0 1
2015-05-12 22:21:58.830 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0.0470588 0 1
2015-05-12 22:21:58.831 Game[6542:220967] Color: UIDeviceRGBColorSpace 0 0.141176 0 1
2015-05-12 22:21:58.831 Game[6542:220967] Color: UIDeviceRGBColorSpace 0.627451 0.694118 0.317647 1
2015-05-12 22:21:58.832 Game[6542:220967] Color: UIDeviceRGBColorSpace 0.756863 0.121569 0 1
2015-05-12 22:21:58.832 Game[6542:220967] Color: UIDeviceRGBColorSpace 0.996078 0 0 1
2015-05-12 22:21:58.832 Game[6542:220967] Color: UIDeviceRGBColorSpace 0.996078 0.00392157 0 1

The fist 32 lines are UIDeviceRGBColorSpace 0 0 0 1, which is correct (Black) however after that there should be four more black pixels a green (#2aff00) and three red (#ff0000) except there are 8 non black pixels, each with a different color value.

第一个32行是UIDeviceRGBColorSpace 0 0 0 1,这是正确的(黑色)然而之后应该有四个黑色像素绿色(#2aff00)和三个红色(#ff0000)除了有8个非黑色像素,每个都有不同的颜色值。

Here is the image which was created in Photoshop:

这是在Photoshop中创建的图像:

Swift  - 读取.jpg图像的像素颜色返回不正确的值

Why aren't any of the red colors the same in RGB and why are there 8 non black pixels?

为什么RGB中没有任何红色相同,为什么有8个非黑色像素?

Thanks in advance, Johann

先谢谢你,约翰

1 个解决方案

#1


Okay so after a fair bit of struggling I tried saving my image as a PNG file. Fixed everything.

好吧,经过一段时间的努力,我尝试将我的图像保存为PNG文件。修复一切。

According to a friend JPG images kind of blur between the pixel colours where as PNG will keep each pixel a solid colour. Using PNG for this type of work is much more reliable. - Excuse me for my non-technical talk.

根据朋友JPG图像在像素颜色之间存在一种模糊,其中PNG将使每个像素保持纯色。使用PNG进行此类工作更加可靠。 - 请原谅我的非技术性谈话。

Thank you.

#1


Okay so after a fair bit of struggling I tried saving my image as a PNG file. Fixed everything.

好吧,经过一段时间的努力,我尝试将我的图像保存为PNG文件。修复一切。

According to a friend JPG images kind of blur between the pixel colours where as PNG will keep each pixel a solid colour. Using PNG for this type of work is much more reliable. - Excuse me for my non-technical talk.

根据朋友JPG图像在像素颜色之间存在一种模糊,其中PNG将使每个像素保持纯色。使用PNG进行此类工作更加可靠。 - 请原谅我的非技术性谈话。

Thank you.