【发布时间】:2014-06-18 15:54:37
【问题描述】:
目标:使用图案图像在 Quartz 2D 中绘制。目前:
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);
}
用于:
CGPatternRef strokePattern = CGPatternCreate(NULL,
CGRectMake(0, 0, 50, 50),
CGAffineTransformIdentity,
50, // horizontal spacing
50, // vertical spacing
kCGPatternTilingNoDistortion,
true,
&kPatternCallbacks);
实现类 (ViewController.m) 中的所有这些东西。完美,我可以画出图案图像。
问题:动态改变C回调函数内部的东西,例如:
在“ViewController.h”中
@interface ViewController : UIViewController {
NSString *imageName;
}
@property (nonatomic, strong) NSString *imageName;
并在实现类(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);
}
还有错误!!! -> 使用未声明的标识符“imageName”
我已经尝试过了……
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);
}
例程结构不能改变,因为它是由 CGPatternCallbacks 定义的。
void routine(void *info, CGContextRef contextRef) { }
我搜索了很多,但没有找到解决方案。像这样的话题:
“将 Objective-C 对象传递给 C 函数”
“混合 Objective-C 和 C”
“回调函数”
“C 回调”
什么都没有,什么都没有。
不可能?我不这么认为!我将不得不放弃...... F ***!
【问题讨论】:
-
您正试图从 C 函数访问您的类的实例变量 - 这是不可能的。只是一个建议 - 在调用
CGPatternCreate之前使用全局变量并将其设置为指向您的视图控制器。这样您就可以在回调中访问该属性(例如 -controller.imageName.CGImage)。 -
谢谢。我找到了。我用了一句话:-> static NSString *imageName = @"PatternImage1";
标签: objective-c c callback