views:

284

answers:

2

Hi,

I am working with CGLayers, and its blend mode constants, as kCGBlendModeDifference, kCGBlendModeHardLight, kCGBlendModeLuminosity, etc.

I would like to build an array out of these constants and use them by index, in an instruction like

CGContextSetBlendMode(context, [myArrayOfBlendModes objectAtIndex:x]);

but I have two problems here:

1) Objective-C will not allow to build an array using

NSMutableArray * myArrayOfBlendModes = [[[NSMutableArray alloc] initWithObjects:
kCGBlendModeDifference,
kCGBlendModeHardLight,
kCGBlendModeLuminosity,
nil] autorelease];

because these constants are not objects. To make things worst, these constants return CGBlend values...

So, the first point is how to make an array of CGBlends...

2) if this is possible, will

[myArrayOfBlendModes objectAtIndex:x]

return a valid value that can be used on

CGContextSetBlendMode(context, [myArrayOfBlendModes objectAtIndex:x]);

???

and there's another question here.. an objective-c code used as an argument to a C routine... is it possible?

These are two puzzles I am trying to solve for hours...

thanks for any help

+3  A: 

These constants are ints, as far as I can tell. If you really want this kind of functionality you can do something like:

CGBlendMode modes[] = {kCGBlendModeDifference, kCGBlendModeHardLight, kCGBlendModeDifference};

Or, if you really really want to use NSArray's, you can just throw these into a NSNumber...

NSNumber *luminous = [NSNumber numberWithInt:kCGBlendModeDifference], *difference = [NSNumber numberWithInt: kCGBlendModeDifference], *hardLight = [NSNumber numberWithInt: kCGBlendModeHardLight];

NSMutableArray * myArrayOfBlendModes =

[[[NSMutableArray alloc] initWithObjects: difference, hardLight, luminous, nil] autorelease];

CGContextSetBlendMode(context, [[myArrayOfBlendModes objectAtIndex:x]

intValue]);

Alex C Schaefer
thanks. I was thinking supposing CGBlends were some sort of variable not integer and wondering how to create such bizarre array of "cgblends"... thanks.
Digital Robot
I'd use a regular C array as @Alex C Schaefer mentioned. As for passing an obj C code to a C function... why? What's the C routine going to do with the obj C "code"? By code, do you mean an Obj C method?If you want a C routine to call an obj C method, then you really have to pass the object to the routine too. An obj C method is meaningless in C without its object. Please clarify what you are trying to do here, maybe we can help.
mahboudz
that's it. I was doing something wrong and now it is clear. thanks.
Digital Robot
A: 

And yes, you can use an obj-c call to feed a c function. Objective C and C are the same dance - Objective C just being a superset of C. It will run the method and send the result back to the c function call no problems.

Alex C Schaefer
As an argument to a C routine? Not sure how you are envisioning this.
mahboudz