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.