簡體   English   中英

如何在CGImage中編輯像素的Alpha

[英]How to edit the alpha of pixels in CGImage

我想拍攝一個CGImage,遍歷像素並更改其alpha值。

我已經看到了更改像素RGB值的代碼,例如該線程。 是否可以通過編程方式反轉圖像的顏色? 它顯示了如何反轉像素的RGB值。

但是我想更改Alpha值。

有什么辦法嗎?

您可以使用顏色選擇器中的代碼來獲取圖像的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。 您的應用可以根據此修改后的位圖創建新圖像。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM