Getting the average RGB color of a CGImageRef in iOS

女生的网名这么多〃 提交于 2019-12-13 00:57:55

问题


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!


回答1:


If you have a CGImage, you get the data by calling

CGDataProviderRef CGImageGetDataProvider (
   CGImageRef image
);

CGImage doc

Then you can copy the data

CFDataRef CGDataProviderCopyData(
   CGDataProviderRef provider
);

CGDataProvider doc

Since CFData is the same as NSData you can cast it and retrieve the bytes

- (const void *)bytes

CFData doc

NSData doc

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.



来源:https://stackoverflow.com/questions/7623455/getting-the-average-rgb-color-of-a-cgimageref-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!