cocoacocos2d-iphoneuiimagepickercontrollercocos2d-iphone-3

Cocos2d V3 can't switch scenes after using the UIImagePickerController Class


I'm having a little trouble switching scenes (or even displaying sprites) on my CCscene UserLevelCreation, but it only happens after i use the UIImagePickerController to take/accept a picture, And then when I try to switch scenes, It crashes with the error:

"Could not attach texture to framebuffer"

this is the code I use to take a picture:

-(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];
}

And the WriteImageToPath function:

-(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---------------------------------------------------------------------------------- I just tried switching scenes in different places in the code, and I can switch scenes freely until the Function:

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

I think that function might be the problem.

So far, I have tried to:

1.switch scenes before taking the picture, and that works fine

2.I read that it might be because i was resizing the image, but commenting that out made no difference.

3.I've also added breakpoints before the scene switch, and the scene isn't nil

4.Finally i tred dismissing the uip UIImagePickerController, but that made no difference either.

Sorry for the long post /: if anyone knows whats going on any help would be really great.


Solution

  • Ok so I found the problem to be this line:

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

    Apparently some Parts of Uikit are not thread-friendly, so I'm now using just

     [self writeImgToPath];
    

    And it works, hope this helps anyone with the same problem.