开发者

Getting the average RGB color of a CGImageRef in iOS

开发者 https://www.devze.com 2023-04-10 04:52 出处:网络
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 CGIma

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 C开发者_如何学GooreGraphics, so I'm not sure where to go from there. Any help is appreciated. Thanks!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号