【问题标题】:How to edit the alpha of pixels in CGImage如何在 CGImage 中编辑像素的 alpha
【发布时间】:2011-12-11 12:01:20
【问题描述】:

我想获取一个 CGImage,遍历像素并更改它们的 alpha 值。

我见过改变像素 RGB 值的代码,比如这个线程 Is programmatically inverting the colors of an image possible? 它展示了如何反转像素的 RGB 值。

但我想更改 alpha 值。

有什么办法吗?

【问题讨论】:

    标签: iphone ios ipad ios5


    【解决方案1】:

    您可以使用来自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)];
    }
    

    【讨论】:

      【解决方案2】:

      图像可能有也可能没有应用程序可访问的像素,或已知大小的像素。但是,您可以将图像渲染为您创建的位图。如果它是 RGBA(或 ABGR)位图,那么您可以像更改 RGB 值一样更改任何像素的 alpha。您的应用可以从修改后的位图创建新图像。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-11-19
        • 1970-01-01
        • 2014-06-07
        • 1970-01-01
        • 1970-01-01
        • 2018-02-19
        • 2015-04-29
        • 2011-11-10
        相关资源
        最近更新 更多