我想要一個CGImage,迭代像素並更改它們的alpha值。如何在CGImage中編輯像素的alpha值
我看過改變像素RGB值的代碼,像這個線程Is programmatically inverting the colors of an image possible? 它演示瞭如何反轉像素的RGB值。
但我想更改alpha值。
有沒有辦法做到這一點?
我想要一個CGImage,迭代像素並更改它們的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。你的應用可以從這個修改的位圖創建一個新的圖像。