获取iOS中CGImageRef的平均RGB颜色 [英] Getting the average RGB color of a CGImageRef in iOS
问题描述
我已经为此苦苦挣扎了两个小时,我希望其他人能对此有所了解。我正在寻找一种获取1x1 UIImage的平均RGB颜色的方法。到目前为止,我已经从UIImage创建了CGImageRef,但是我真的是CoreGraphics的新手,所以我不确定从那里去哪里。任何帮助表示赞赏。谢谢!
I've been struggling with this for a couple of hours, and I'm hoping someone else has some insight. I'm looking for a way to get the average RGB color of a 1x1 UIImage. So far I've created a CGImageRef from the UIImage, but I'm really new to CoreGraphics, so I'm not sure where to go from there. Any help is appreciated. Thanks!
推荐答案
如果您有CGImage,则可以通过调用
If you have a CGImage, you get the data by calling
CGDataProviderRef CGImageGetDataProvider (
CGImageRef image
);
然后您就可以复制数据
CFDataRef CGDataProviderCopyData(
CGDataProviderRef provider
);
由于 CFData
与 NSData
相同,可以将其强制转换并检索字节
Since CFData
is the same as NSData
you can cast it and retrieve the bytes
- (const void *)bytes
现在您有了原始字节,可以使用它们进行任何操作,使用
Now you have the raw bytes, you can do anything with them, use
CGImageAlphaInfo CGImageGetAlphaInfo (
CGImageRef image
);
CGBitmapInfo CGImageGetBitmapInfo (
CGImageRef image
);
获取信息如何将图像数据存储在所获取的字节中。
to get information how is the image data stored in the bytes you got.
这篇关于获取iOS中CGImageRef的平均RGB颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!