Skip to content

Instantly share code, notes, and snippets.

@ygit
Created November 3, 2015 08:18
Show Gist options
  • Save ygit/ff8b591b7373ba740a04 to your computer and use it in GitHub Desktop.
Save ygit/ff8b591b7373ba740a04 to your computer and use it in GitHub Desktop.
Get Average Color of UIImage
- (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