Getting a Black and White UIImage (Not Grayscale)

前端 未结 6 1701
独厮守ぢ
独厮守ぢ 2021-02-04 20:23

I need to get a pure black and white UIImage from another UIImage (not grayscale). Anyone can help me?

Thanks for reading.

EDITED:

Here

6条回答
  •  灰色年华
    2021-02-04 20:55

    The code worked right for me,just need some tweak ...here are few changes I made to work it properly by assigning value to dataBitmap[] array's zeroth index...

         for (int i = 0; i < image.size.width * image.size.height * 4; i += 4) {
                //here an index for zeroth element is assigned
                if ((dataBitmap[i + 0]+dataBitmap[i + 1] + dataBitmap[i + 2] + dataBitmap[i + 3]) < (255 * 4 / 2)) {   
             //  multiply four,instead of three              
                    dataBitmap[i + 0] = 0;
                    dataBitmap[i + 1] = 0;
                    dataBitmap[i + 2] = 0;
                    dataBitmap[i + 3] = 0;
                } else {
                    dataBitmap[i + 0] = 255;
                    dataBitmap[i + 1] = 255;
                    dataBitmap[i + 2] = 255;
                    dataBitmap[i + 3] = 255;
                }
            }
    

    Hope it will work.

提交回复
热议问题