在CGContext iOS中获取像素的RGB值

时间:2015-03-08 17:23:54

标签: ios swift uiimage rgb cgimage

我正在尝试从一个UIImageView到另一个UIImageView执行一个非常简单的图像映射,但是通过CGContext,这样我就可以操作像素RGB值。这是我使用的代码,它允许我浏览初始图像的像素并创建输出图像,但我不知道如何提取像素RBG值。任何人都可以提出建议吗?

var sampleImage = UIImage()

func map(){
    self.sampleImage = imageViewInput.image!
    var imageRect = CGRectMake(0, 0, self.sampleImage.size.width, self.sampleImage.size.height)
    UIGraphicsBeginImageContext(sampleImage.size)
    var context: CGContextRef = UIGraphicsGetCurrentContext()

    CGContextSaveGState(context)
    CGContextDrawImage(context, imageRect, sampleImage.CGImage)

    var pixelCount = 0
    for var i = CGFloat(0); i<sampleImage.size.width; i++ {
        for var j = CGFloat(0); j<sampleImage.size.height; j++ {

            pixelCount++
            var index = pixelCount

            var red: CGFloat = // Get the red component value of the pixel
            var green: CGFloat = // Get the green component value of the pixel
            var blue: CGFloat = // Get the blue component value of the pixel

            CGContextSetRGBFillColor(context, red, green, blue, 1)
            CGContextFillRect(context, CGRectMake(i, j, 1, 1))

        }
    }

    CGContextRestoreGState(context)
    var img: UIImage = UIGraphicsGetImageFromCurrentImageContext()
    imageViewOutput.image = img

}

0 个答案:

没有答案