Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取CGIOS中像素的RGB值_Ios_Swift_Uiimage_Rgb_Cgimage - Fatal编程技术网

获取CGIOS中像素的RGB值

获取CGIOS中像素的RGB值,ios,swift,uiimage,rgb,cgimage,Ios,Swift,Uiimage,Rgb,Cgimage,我试图通过CGContext将图像从一个UIImageView映射到另一个UIImageView,这样我就可以操纵像素RGB值。这是我使用的代码,它允许我遍历初始图像的像素并创建输出图像,但我不知道如何提取像素RBG值。有人能建议怎么做吗 var sampleImage = UIImage() func map(){ self.sampleImage = imageViewInput.image! var imageRect = CGRectMake(0, 0, self.sa

我试图通过CGContext将图像从一个UIImageView映射到另一个UIImageView,这样我就可以操纵像素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

}

如果你能得到像素的CGColor,你可以使用CGColorGetComponents从中获得rgb值。参见苹果。底线是,创建一个上下文,将图像绘制到该上下文中,使用CGBitmapContextGetData访问像素缓冲区。然后可以从该像素数据创建新图像。参见Swift示例。