【发布时间】:2011-12-11 12:01:20
【问题描述】:
我想获取一个 CGImage,遍历像素并更改它们的 alpha 值。
我见过改变像素 RGB 值的代码,比如这个线程 Is programmatically inverting the colors of an image possible? 它展示了如何反转像素的 RGB 值。
但我想更改 alpha 值。
有什么办法吗?
【问题讨论】:
我想获取一个 CGImage,遍历像素并更改它们的 alpha 值。
我见过改变像素 RGB 值的代码,比如这个线程 Is programmatically inverting the colors of an image possible? 它展示了如何反转像素的 RGB 值。
但我想更改 alpha 值。
有什么办法吗?
【问题讨论】:
您可以使用来自color picker 的代码来获取图像的 RGBA 矩阵并更改 alpha。并保存到图像回来。
https://github.com/sakrist/VBColorPicker/blob/master/VBColorPicker/VBColorPicker.m
unsigned char* data = CGBitmapContextGetData (cgctx);
if (data != NULL && data != 0) {
//offset locates the pixel in the data from x,y.
//4 for 4 bytes of data per pixel, w is width of one row of data.
int offset = 4*((w*round(point.y))+round(point.x));
int alpha = data[offset];
int red = data[offset+1];
int green = data[offset+2];
int blue = data[offset+3];
NSLog(@"offset: %i colors: RGB A %i %i %i %i",offset,red,green,blue,alpha);
color = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue:(blue/255.0f) alpha:(alpha/255.0f)];
}
【讨论】:
图像可能有也可能没有应用程序可访问的像素,或已知大小的像素。但是,您可以将图像渲染为您创建的位图。如果它是 RGBA(或 ABGR)位图,那么您可以像更改 RGB 值一样更改任何像素的 alpha。您的应用可以从修改后的位图创建新图像。
【讨论】: