Created
September 24, 2012 15:02
-
-
Save yetithefoot/3776411 to your computer and use it in GitHub Desktop.
UIImage average color
This file contains hidden or 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
@implementation UIImage (AverageColor) | |
- (UIColor *)averageColor { | |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); | |
unsigned char rgba[4]; | |
CGContextRef context = CGBitmapContextCreate(rgba, 1, 1, 8, 4, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); | |
CGContextDrawImage(context, CGRectMake(0, 0, 1, 1), self.CGImage); | |
CGColorSpaceRelease(colorSpace); | |
CGContextRelease(context); | |
if(rgba[3] > 0) { | |
CGFloat alpha = ((CGFloat)rgba[3])/255.0; | |
CGFloat multiplier = alpha/255.0; | |
return [UIColor colorWithRed:((CGFloat)rgba[0])*multiplier | |
green:((CGFloat)rgba[1])*multiplier | |
blue:((CGFloat)rgba[2])*multiplier | |
alpha:alpha]; | |
} | |
else { | |
return [UIColor colorWithRed:((CGFloat)rgba[0])/255.0 | |
green:((CGFloat)rgba[1])/255.0 | |
blue:((CGFloat)rgba[2])/255.0 | |
alpha:((CGFloat)rgba[3])/255.0]; | |
} | |
} | |
@end |
This file contains hidden or 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
#import <UIKit/UIKit.h>; | |
@interface UIImage (AverageColor) | |
- (UIColor *)averageColor; | |
@end | |
#import "UIImage+AverageColor.h" | |
@implementation UIImage (AverageColor) | |
- (UIColor *)averageColor { | |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); | |
unsigned char rgba[4]; | |
CGContextRef context = CGBitmapContextCreate(rgba, 1, 1, 8, 4, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); | |
CGContextDrawImage(context, CGRectMake(0, 0, 1, 1), self.CGImage); | |
CGColorSpaceRelease(colorSpace); | |
CGContextRelease(context); | |
if(rgba[3] > 0) { | |
CGFloat alpha = ((CGFloat)rgba[3])/255.0; | |
CGFloat multiplier = alpha/255.0; | |
return [UIColor colorWithRed:((CGFloat)rgba[0])*multiplier | |
green:((CGFloat)rgba[1])*multiplier | |
blue:((CGFloat)rgba[2])*multiplier | |
alpha:alpha]; | |
} | |
else { | |
return [UIColor colorWithRed:((CGFloat)rgba[0])/255.0 | |
green:((CGFloat)rgba[1])/255.0 | |
blue:((CGFloat)rgba[2])/255.0 | |
alpha:((CGFloat)rgba[3])/255.0]; | |
} | |
} | |
@end |
@nissaba that's because the greater than symbol was encoded. Should read:
if(rgba[3] > 0) {
I'm having a memory leak with this line:
CGContextDrawImage(context, CGRectMake(0, 0, 1, 1), self.CGImage);
I would think that CGContextRelease would take care of it, but it doesn't so each time I call the method I add 30MB+ to my allocation. That lasts about 6 calls before iOS kills me.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if (rgba[3] & gt; 0)
this generates errors under Xcode 6.3
Use of undeclared identifier 'gt'
Expected ';' after expression