gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:51:08 24 4
gpt4 key购买 nike

我在 CCscene UserLevelCreation 上切换场景(甚至显示 Sprite )时遇到了一些麻烦,但只有在我使用 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]];
}

-更新--------------------------------------------------------- ------------------------------------------------我只是尝试在代码中的不同地方切换场景,并且可以自由切换场景,直到Function:

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

我认为该功能可能是问题所在。

到目前为止,我已经尝试过:

1.拍照前切换场景,效果很好

2.我读到这可能是因为我正在调整图像大小,但注释掉没有什么区别。

3.我还在场景切换前添加了断点,并且场景不为nil

4.最后我尝试放弃 uip UIImagePickerController,但这也没有什么区别。

抱歉发了这么长的帖子/:如果有人知道发生了什么事,任何帮助都会很棒。

最佳答案

好吧,我发现问题出在这一行:

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

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

 [self writeImgToPath];

它确实有效,希望这对遇到同样问题的人有所帮助。

关于cocoa - Cocos2d V3使用UIImagePickerController类后无法切换场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26331069/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com