如何使用CGContext获得像素的RGB值? [英] How do I get the RGB Value of a pixel using CGContext?
问题描述
我正在尝试通过更改像素来编辑图像.
我有以下代码:
let imageRect = CGRectMake(0,0,self.image.image!.size.width,self.image.image!.size.height)UIGraphicsBeginImageContext(self.image.image!.size)让上下文= UIGraphicsGetCurrentContext()CGContextSaveGState(上下文)CGContextDrawImage(context,imageRect,self.image.image!.CGImage)对于x in 0 ... Int(self.image.image!.size.width){y in 0 ... Int(self.image.image!.size.height){var red = 0如果y%2 == 0 {红色= 255}CGContextSetRGBFillColor(context,CGFloat(red/255),0.5,0.5,1)CGContextFillRect(上下文,CGRectMake(CGFloat(x),CGFloat(y),1,1))}}CGContextRestoreGState(上下文)self.image.image = UIGraphicsGetImageFromCurrentImageContext()
我正在遍历所有像素并更改每个像素的值,然后将其转换回图像.我想要做的就是以某种方式获取当前像素的值(在y-for循环中)并对该数据进行一些处理.我没有在互联网上找到有关此特定问题的任何信息.
好吧,我想我有一个应该可以在Swift 2中使用的解决方案.
对于以下UIColor扩展,请
对此:
这对我在操场上有效-您所需要做的就是将其复制并粘贴到操场上以查看结果:
//:游乐场-名词:人们可以玩耍的地方导入UIKit导入XCPlayground扩展名CALayer {func colorOfPoint(point:CGPoint)->UIColor{var pixel:[CUnsignedChar] = [0,0,0,0]让colorSpace = CGColorSpaceCreateDeviceRGB()让bitmapInfo = CGBitmapInfo(rawValue:CGImageAlphaInfo.PremultipliedLast.rawValue)let context = CGBitmapContextCreate(& pixel,1,1,8,4,colorSpace,bitmapInfo.rawValue)CGContextTranslateCTM(上下文,-point.x,-point.y)self.renderInContext(上下文!)让红色:CGFloat = CGFloat(pixel [0])/255.0让绿色:CGFloat = CGFloat(pixel [1])/255.0让蓝色:CGFloat = CGFloat(pixel [2])/255.0令alpha:CGFloat = CGFloat(pixel [3])/255.0//println(点颜色-红色:\(红色)绿色:\(绿色)蓝色:\(蓝色)")let color = UIColor(红色:红色,绿色:绿色,蓝色:蓝色,alpha:alpha)返回颜色}}扩展UIColor {var组件:(红色:CGFloat,绿色:CGFloat,蓝色:CGFloat,alpha:CGFloat){var r:CGFloat = 0var g:CGFloat = 0var b:CGFloat = 0var a:CGFloat = 0getRed(& r,绿色:& g,蓝色:& b,alpha:& a)返回(r,g,b,a)}}//获取我们可以处理的图像var imageFromURL = UIImage(data:NSData(contentsOfURL:NSURL(string:"https://www.gravatar.com/avatar/ba4178644a33a51e928ffd820269347c?s=328&d=identicon&r=PG&f=1")!)!)//仅使用该图像的一小部分-50 x 50平方let imageSliceArea = CGRectMake(0,0,50,50);让imageSlice = CGImageCreateWithImageInRect(imageFromURL?.CGImage,imageSliceArea);//我们将处理这张图片var image = UIImage(CGImage:imageSlice!)让imageView = UIImageView(image:image)//测试点(0,0)上的扩展-返回r 0.541 g 0.78 b 0.227 a 1.0var pointColor = imageView.layer.colorOfPoint(CGPoint(x:0,y:0))让imageRect = CGRectMake(0,0,image.size.width,image.size.height)UIGraphicsBeginImageContext(image.size)让上下文= UIGraphicsGetCurrentContext()CGContextSaveGState(上下文)CGContextDrawImage(context,imageRect,image.CGImage)对于x in 0 ... Int(image.size.width){y in 0 ... Int(image.size.height){var pointColor = imageView.layer.colorOfPoint(CGPoint(x:x,y:y))//我在这里运用了自己的创造力-将其更改为您想要的任何逻辑如果y%2 == 0 {CGContextSetRGBFillColor(context,pointColor.components.red,0.5,0.5,1)}别的 {CGContextSetRGBFillColor(context,255,0.5,0.5,1)}CGContextFillRect(上下文,CGRectMake(CGFloat(x),CGFloat(y),1,1))}}CGContextRestoreGState(上下文)图片= UIGraphicsGetImageFromCurrentImageContext()
我希望这对您有用.我玩这个很开心!
I'm trying to edit images by changing the pixels.
I have the following code:
let imageRect = CGRectMake(0, 0, self.image.image!.size.width, self.image.image!.size.height)
UIGraphicsBeginImageContext(self.image.image!.size)
let context = UIGraphicsGetCurrentContext()
CGContextSaveGState(context)
CGContextDrawImage(context, imageRect, self.image.image!.CGImage)
for x in 0...Int(self.image.image!.size.width) {
for y in 0...Int(self.image.image!.size.height) {
var red = 0
if y % 2 == 0 {
red = 255
}
CGContextSetRGBFillColor(context, CGFloat(red/255), 0.5, 0.5, 1)
CGContextFillRect(context, CGRectMake(CGFloat(x), CGFloat(y), 1, 1))
}
}
CGContextRestoreGState(context)
self.image.image = UIGraphicsGetImageFromCurrentImageContext()
I'm looping through all the pixels and changing the value of the each pixel, then converting it back to an image. Want I want to do is somehow get the value of the current pixel (in the y-for-loop) and do something with that data. I have not found anything on the internet about this particular problem.
Ok, I think I have a solution that should work for you in Swift 2.
Credit goes to this answer for the UIColor extension below.
Since I needed an image to test this on I chose a slice (50 x 50 - top left corner) of your gravatar...
So the code below converts this:
To this:
This works for me in a playground - all you should have to do is copy and paste into a playground to see the result:
//: Playground - noun: a place where people can play
import UIKit
import XCPlayground
extension CALayer {
func colorOfPoint(point:CGPoint) -> UIColor
{
var pixel:[CUnsignedChar] = [0,0,0,0]
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue)
let context = CGBitmapContextCreate(&pixel, 1, 1, 8, 4, colorSpace,bitmapInfo.rawValue)
CGContextTranslateCTM(context, -point.x, -point.y)
self.renderInContext(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
//println("point color - red:\(red) green:\(green) blue:\(blue)")
let color = UIColor(red:red, green: green, blue:blue, alpha:alpha)
return color
}
}
extension UIColor {
var components:(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
var r:CGFloat = 0
var g:CGFloat = 0
var b:CGFloat = 0
var a:CGFloat = 0
getRed(&r, green: &g, blue: &b, alpha: &a)
return (r,g,b,a)
}
}
//get an image we can work on
var imageFromURL = UIImage(data: NSData(contentsOfURL: NSURL(string:"https://www.gravatar.com/avatar/ba4178644a33a51e928ffd820269347c?s=328&d=identicon&r=PG&f=1")!)!)
//only use a small area of that image - 50 x 50 square
let imageSliceArea = CGRectMake(0, 0, 50, 50);
let imageSlice = CGImageCreateWithImageInRect(imageFromURL?.CGImage, imageSliceArea);
//we'll work on this image
var image = UIImage(CGImage: imageSlice!)
let imageView = UIImageView(image: image)
//test out the extension above on the point (0,0) - returns r 0.541 g 0.78 b 0.227 a 1.0
var pointColor = imageView.layer.colorOfPoint(CGPoint(x: 0, y: 0))
let imageRect = CGRectMake(0, 0, image.size.width, image.size.height)
UIGraphicsBeginImageContext(image.size)
let context = UIGraphicsGetCurrentContext()
CGContextSaveGState(context)
CGContextDrawImage(context, imageRect, image.CGImage)
for x in 0...Int(image.size.width) {
for y in 0...Int(image.size.height) {
var pointColor = imageView.layer.colorOfPoint(CGPoint(x: x, y: y))
//I used my own creativity here - change this to whatever logic you want
if y % 2 == 0 {
CGContextSetRGBFillColor(context, pointColor.components.red , 0.5, 0.5, 1)
}
else {
CGContextSetRGBFillColor(context, 255, 0.5, 0.5, 1)
}
CGContextFillRect(context, CGRectMake(CGFloat(x), CGFloat(y), 1, 1))
}
}
CGContextRestoreGState(context)
image = UIGraphicsGetImageFromCurrentImageContext()
I hope this works for you. I had fun playing around with this!
这篇关于如何使用CGContext获得像素的RGB值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!