如何判断 UIImage 是彩色还是单色?

发布于 2024-12-18 20:00:43 字数 104 浏览 1 评论 0原文

有没有办法确定 UIImage/(或任何其他格式的图像)是彩色还是单色?我的应用程序打算读取 UIImage(存储在照片应用程序中的图像),然后根据其色阶(彩色图像或灰度)对图像进行不同的处理。

Is there a way to find out if a UIImage/(Or the image in any other format) is colored or monochrome? My app intends to read a UIImage(image stored in the Photo app), and then do different set of processing on the image, based on its colorscale (colored image or gray scale).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

囚我心虐我身 2024-12-25 20:00:43

您正在寻找的是 UIImage 的颜色空间。您可以在此处找到答案。

单色空间是

CFStringRef kCGColorSpaceGenericGray
CFStringRef kCGColorSpaceGenericGrayGamma2_2
CFStringRef k​CGColor​Space​Extended​Gray

任何其他颜色。

What you are looking for is the color space of the UIImage. You can find the answer here.

The mono color spaces are

CFStringRef kCGColorSpaceGenericGray
CFStringRef kCGColorSpaceGenericGrayGamma2_2
CFStringRef k​CGColor​Space​Extended​Gray

Anything else is color.

我还不会笑 2024-12-25 20:00:43
if (CGColorSpaceGetModel(CGImageGetColorSpace(myUIImage.CGImage)) == kCGColorSpaceModelMonochrome) {
    // Image is grayscale
} else {
    // Image allows color
}

请注意,“彩色”图像可能仍然只使用灰色阴影!单色图像可以以支持颜色的格式存储。

if (CGColorSpaceGetModel(CGImageGetColorSpace(myUIImage.CGImage)) == kCGColorSpaceModelMonochrome) {
    // Image is grayscale
} else {
    // Image allows color
}

Note that a "color" image might still only use shades of gray! A monochrome image can be stored in a color-supporting format.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文