我有一个普通的UIKit应用程序,但我想使用cocos2d做一点工作。我想使用函数glReadPixels,因为我需要一个cocos2d图像 - > CCSprite。
我现在正在解决这个问题2天,因为我收到了这个错误:
2012-08-08 16:58:50.673 iBlaulicht 2 [11801:16a03] *由于未捕获的异常'NSInvalidArgumentException'而终止应用,原因:' + [NSString stringWithCString:encoding:]:NULL cString' * *第一次抛出调用堆栈: (0x1f88022 0x1b9bcd6 0x1f30a48 0x1f309b9 0x16050c7 0x62687 0x62589 0x62317 0x86979 0x47c5c 0x2d63 0xdc5a1e 0x222d 0xcfc386 0xcfd274 0xd0c183 0xd0cc38 0xd00634 0x338cef5 0x1f5c195 0x1ec0ff2 0x1ebf8da 0x1ebed84 0x1ebec9b 0xcfcc65 0xcfe626 0x20cb 0x2065) 终止调用抛出异常(lldb)
这是我的代码:
- (id)initWithImage:(UIImage *)drawnImage{
if (self == [super init]) {
CCTexture2D *tex = [[[CCTexture2D alloc] initWithImage:drawnImage resolutionType:kCCResolutionRetinaDisplay] autorelease];
CCSprite *imageSprite = [CCSprite spriteWithTexture:tex];
imageSprite.position = CGPointMake(0, 0 );
[self addChild:imageSprite];
}
return self;
}
- (NSArray *)determinePixels{
for (int y_direction = 0; currentImage.size.height > y_direction; y_direction += 20) {
for (int x_direction = 0; currentImage.size.width > x_direction; x_direction += 20) {
Byte pixelColor[4];
glReadPixels(x_direction, y_direction, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &pixelColor[0]);
// Just to test
NSLog(@"0");
}
}
return 0;
}
我感谢任何帮助......谢谢。 克里斯
答案 0 :(得分:1)
看看Ray Wenderlich关于这个主题的帖子;
http://www.raywenderlich.com/4817/how-to-integrate-cocos2d-and-uikit
答案 1 :(得分:1)
我自己找到了解决方案!
我添加了一个EAGLView并使用CCDirector将其设置为OpenGl View。
- (id)initWithImage:(UIImage *)drawnImage{
if (self == [super init]) {
EAGLView *glview = [EAGLView viewWithFrame:CGRectMake(0, 0, 250,350)];
CCDirector *director = [CCDirector sharedDirector];
[director setOpenGLView:glview];
CCTexture2D *tex = [[CCTexture2D alloc] initWithImage:drawnImage resolutionType:kCCResolutionRetinaDisplay];
imageSprite = [KKPixelMaskSprite spriteWithTexture:tex];
imageSprite.position = CGPointMake(0, 0 );
[self addChild:imageSprite];
}
return self;
}