使用UIImagePickerController类后,Cocos2d V3无法切换场景

时间:2014-10-12 23:29:15

标签: cocoa cocos2d-iphone uiimagepickercontroller cocos2d-iphone-3

我在CCscene UserLevelCreation上切换场景(甚至显示精灵)时遇到了一些麻烦,但只有在我使用UIImagePickerController接收/接受图片后才会发生,然后当我尝试切换场景时,它崩溃了错误:

"Could not attach texture to framebuffer"

这是我用来拍照的代码:

-(void)takePhoto{
AppController *appdel = (AppController*) [[UIApplication sharedApplication] delegate];
@try {

    uip = [[UIImagePickerController alloc] init] ;
    uip.sourceType = UIImagePickerControllerSourceTypeCamera;
    uip.allowsEditing = YES;
    uip.delegate = self;
}
@catch (NSException * e) {
    uip = nil;
}
@finally {
    if(uip) {
        [appdel.navController presentModalViewController:uip animated:NO];
    }
}
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSString * LevelImage;
LevelImage=[info objectForKey:UIImagePickerControllerOriginalImage];
AppController *appdel = (AppController*) [[UIApplication sharedApplication] delegate];
[appdel.navController dismissModalViewControllerAnimated:YES];

[NSThread detachNewThreadSelector:@selector(writeImgToPath:) toTarget:self withObject:LevelImage];
}

WriteImageToPath函数:

-(void)writeImgToPath:(id)sender
{
UIImage *image = sender;
NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                       NSUserDomainMask,
                                                       YES);
CGSize size;
int currentProfileIndex = 1;
NSString *path = [[pathArr objectAtIndex:0]
                  stringByAppendingPathComponent:[NSString stringWithFormat:@"CustomLevel_%d.png",currentProfileIndex]];

size = CGSizeMake(1136, 640);
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, 1136, 640)];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSData *data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
CCLOG(@"saved...");

CGRect r = CGRectMake(0, 0, 1136, 640);
UIGraphicsBeginImageContext(r.size);

UIImage *img1;

[image drawInRect:r];
img1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(img1, nil, nil, nil);
currentProfileIndex++;
[[CCDirector sharedDirector] replaceScene:[LevelEditor Scene]
                           withTransition:[CCTransition transitionPushWithDirection:CCTransitionDirectionLeft duration:1.0f]];
}

-UPDATE --------------------------------------------- ------------------------------------- 我只是尝试在代码中的不同位置切换场景,我可以自由切换场景,直到功能:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

我认为这个功能可能就是问题。

到目前为止,我试图:

1.拍照之前的切换场景,并且工作正常

2.我读到这可能是因为我正在调整图像的大小,但是评论这一点没有任何区别。

3.我还在场景切换前添加了断点,场景不是

4.最后我解雇了uip UIImagePickerController,但这也没有区别。

对不起,很长的帖子/:如果有人知道什么会有任何帮助真的很棒。

1 个答案:

答案 0 :(得分:1)

好的,所以我发现问题是这一行:

[NSThread detachNewThreadSelector:@selector(writeImgToPath:) toTarget:self withObject:LevelImage];
}

显然Uikit的某些部分不是线程友好的,所以我现在只使用

 [self writeImgToPath];

它有效,希望这可以帮助任何有同样问题的人。