更改像素的颜色 [英] Changing the color of a pixel

查看:99
本文介绍了更改像素的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够在给定的CGPoint上读取特定的像素,但是我一直在寻求改变像素的颜色,如果有人可以通过代码片段帮助我,将不胜感激.

i am able to read a particular pixel at given CGPoint but i have been looking to change the color of a pixel and it would be highly appreciated if anyone can help me out with a code snippet.

我的代码是:

unsigned char* data = CGBitmapContextGetData (cgctx);       

if (data != NULL) {    
  offset = 4*((w*round(point.y))+round(point.x));    
  alpha =  data[offset];     
  red = data[offset+1];             
  green = data[offset+2];           
  blue = data[offset+3];     
  color = [UIColor colorWithRed:(red/255.0f) 
                   green:(green/255.0f) blue:(blue/255.0f) 
                   alpha:(alpha/255.0f)];       
}

推荐答案

目前尚不清楚您要做什么.如果要在原始CGImageRef中更改像素的颜色,则可以使用以下方法:

It's not clear what you are trying to do. If you want to change the pixel's color in the original CGImageRef then you would use something like:


// Set the color of the pixel to 50% grey + 50% alpha
data[offset+0] = 128;
data[offset+1] = 128;
data[offset+2] = 128;
data[offset+3] = 128;

// Create a CGBitmapImageContext 
CGContextRef bitmapContext = CGBitmapContextCreate(data, width, height, CGImageGetBitsPerComponent(), width * 4, CGImageGetColorSpace(), kCGImageAlphPremultipliedFirst);

// Draw the bitmap context back to your original context
CGContextDrawImage(bitmapContext, CGMakeRect(...), cgctx); 

您应该一次对数据*进行所有更改,然后将修改后的位图缓冲区写回到原始上下文中.

You should make all of your changes to the data* at once and then write the modified bitmap buffer back to the original context.

这篇关于更改像素的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆