简体   繁体   中英

iPhone iOS how to instantiate a black and white CGColorSpaceRef?

I'm working with this excellent example of converting an image to grayscale: Convert Image to B&W problem CGContext - iPhone Dev

However, for my purposes, I would like to have only pure black and pure white left in the image.

It appears that to do so, I need to pass a black and white color space to the recolor method using a call:

CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(/*black and white name*/);

However, I was unable to find the proper iOS color space names. What I found was from Mac, and the "color space names" referenced from the iOS docs does not point anywhere.

How can I properly create a black and white CGColorSpaceRef?

Thank you!

I am not familiar with a black and white only color space but what you can do is calculate the total average RGB value from all the pixels (lets call it totalAvg ) and use it as a threshold. Meaning for each pixel if its rgb average is greater than the calculated totalAvg than set it to pure white, otherwise set it to pure black.

I agree it is a bit of more work but thats whay I can think of unless you find the colorspace you are looking for.

您可以尝试创建灰色颜色空间,然后创建具有两种颜色(显然是黑色和白色) 的索引颜色空间,然后使用该颜色空间

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM