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

时间:2015-05-13 01:00:46

标签: image swift colors jpeg photoshop

我正试图从swpg中的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!)
        }
    }
}

我在这里创建了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)
    }  
}

显然输出非常长但是这里是前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

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

以下是在Photoshop中创建的图像:

enter image description here

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

先谢谢,约翰

1 个答案:

答案 0 :(得分:1)

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

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

谢谢。