Created
November 3, 2015 08:18
-
-
Save ygit/ff8b591b7373ba740a04 to your computer and use it in GitHub Desktop.
Get Average Color of UIImage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (UIColor *)getAverageColor:(UIImage *)image{ | |
CGSize size = {1, 1}; | |
UIGraphicsBeginImageContext(size); | |
CGContextRef ctx = UIGraphicsGetCurrentContext(); | |
CGContextSetInterpolationQuality(ctx, kCGInterpolationMedium); | |
[image drawInRect:(CGRect){.size = size} blendMode:kCGBlendModeCopy alpha:1]; | |
uint8_t *data = CGBitmapContextGetData(ctx); | |
UIColor *color = [UIColor colorWithRed:data[2] / 255.0f | |
green:data[1] / 255.0f | |
blue:data[0] / 255.0f | |
alpha:1]; | |
UIGraphicsEndImageContext(); | |
return color; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment