1

I am developing an iOS App that needs to recognize colors and other stuff. I got this code:

- (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];
}
}

from this guy: Link

It's 99% perfect, except from this line:

    CGContextDrawImage(context, CGRectMake(0, 0, 1, 1), self.CGImage);

In which I get the following error message:

Property 'CGImage' not found on object of type 'MyClass *'

As far as I know, the CGImage is part of the CoreImage Framework, which is correctly added on the project, and (I believe so) correctly imported in the class. Check out my imports.

#import "MyClass.h"
#import "TiUtils.h"
#import "TiViewProxy.h"
#import <TitaniumKit/TiBlob.h>
#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h>
#import <CoreImage/CIImage.h>

Please, what is wrong? What can I do for the error to go away? Thanks in advance!

gbossa
  • 377
  • 1
  • 3
  • 18

1 Answers1

1

The code is (most likely) a category (in Swift nomenclature 'extension' ) on UIImage.

Technically it would work on anything that responded to the selector CGImage.

The full declaration should be something like:

UIImage + ColorHelp.h

@interface UIImage (ColorHelp)

- (UIColor *)averageColor;

@end 

UIImage + ColorHelp.m

@implementation UIImage (ColorHelp)

- (UIColor *)averageColor {
   //stuff
}

@end 

Create a new "Objective-C File" in Xcode to create a template category on UIImage.

Fromm there all instances of UIImage will respond to average color. e.g

UIImage *myImage = <something>;
UIColor *averageColor = [myImage averageColor];
Warren Burton
  • 17,451
  • 3
  • 53
  • 73
  • Thanks for the reply! So, I have to create the template, and then implement my method inside the .m file. And then I have to use it on other classes creating an object reference to it? Right? – gbossa Jul 08 '20 at 13:53
  • If you implement the code as described **any** instance of `UIImage` will answer to `averageColor`. I assume somewhere in `MyClass` you have a reference to the `UIImage` you are trying to analyse? – Warren Burton Jul 08 '20 at 14:05
  • Yes, that's right. I'm using an instance of it on MyClass, and it's working perfectly now. Thanks man! – gbossa Jul 08 '20 at 14:51
  • If you can (and want to), please upvote my question =) – gbossa Jul 08 '20 at 14:52