The target: to use patterns images to draw in Quartz 2D. At the present moment:
const CGPatternCallbacks kPatternCallbacks = {0, routine, NULL};
void routine(void *info, CGContextRef contextRef) {
UIImage *brushTexture = [UIImage imageNamed:@"PatternImage1"];
CGContextDrawImage(contextRef, CGRectMake(0, 0, 50, 50), brushTexture.CGImage);
}
To use in:
CGPatternRef strokePattern = CGPatternCreate(NULL,
CGRectMake(0, 0, 50, 50),
CGAffineTransformIdentity,
50, // horizontal spacing
50, // vertical spacing
kCGPatternTilingNoDistortion,
true,
&kPatternCallbacks);
All this stuff inside implementation class (ViewController.m). And perfect, I can draw a pattern image.
The problem: to change something inside C callback function dynamically, in example:
In "ViewController.h"
@interface ViewController : UIViewController {
NSString *imageName;
}
@property (nonatomic, strong) NSString *imageName;
And in the implementation class (ViewController.m)
@synthesize imageName;
imageName = @"PatternImage1";
void routine(void *info, CGContextRef contextRef) {
//UIImage *brushTexture = [UIImage imageNamed:@"PatternImage1"];
//CGContextDrawImage(contextRef, CGRectMake(0, 0, 50, 50), brushTexture.CGImage);
CGContextDrawImage(contextRef, CGRectMake(0, 0, 50, 50), imageName.CGImage);
}
And ERROR!!! -> Use of undeclared identifier 'imageName'
And I've already tried...
void routine(void *info, CGContextRef contextRef) {
//NSString *imageName = CFBridgingRelease(info); //<- not working
//NSString *imageName = (__bridge NSString *)(info); //<- not working
NSString *imageName = (__bridge NSString *) info; //<- not working
//UIImage *brushTexture = [UIImage imageNamed:@"PatternImage1"];
CGContextDrawImage(contextRef, CGRectMake(0, 0, 50, 50), imageName.CGImage);
}
The routine structure can not change because it is defined by CGPatternCallbacks.
void routine(void *info, CGContextRef contextRef) { }
I searched a lot but have not found the solution. Topics like:
"Pass an Objective-C object to C function"
"Mix Objective-C and C"
"Callback function"
"C Callbacks"
Nothing, nothing and nothing.
Impossible? I do not think so! I will have to abandon... F***!
CGPatternCreate. That way you'll be able to access the property in the callback (ex. -controller.imageName.CGImage).