Getting the average RGB color of a CGImageRef in i

2019-07-22 15:28发布

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条回答
We Are One
2楼-- · 2019-07-22 16:09

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.

查看更多
登录 后发表回答