UIImagePickerController只工作一次

时间:2012-01-18 02:17:02

标签: objective-c ios cocos2d-iphone

我正在使用UIImagePickerController让用户在我的Cocos2D游戏中选择背景。它在您第一次使用时有效,但如果您尝试再次更改背景,则会显示选择器,但是当您选择图像时,背景将保持不变。

这是我的代码:

- (void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSArray *TouchesA = touches.allObjects;

    for (int i = 0; i < TouchesA.count; i++) {


        CGPoint touchLocation = [self convertTouchToNodeSpace:[TouchesA objectAtIndex:i]];

        CGRect rect = CGRectMake(touchLocation.x, touchLocation.y, 1, 1);

        if(CGRectIntersectsRect(rect, infoButton.boundingBox))
        {
            [self pickPhoto:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
        }
    }
}

-(void)pickPhoto:(UIImagePickerControllerSourceType)sourceType{

    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.delegate = self;
    picker.sourceType = sourceType;
    //picker.wantsFullScreenLayout = YES;
    //[picker presentModalViewController:picker animated:YES];

    [[[CCDirector sharedDirector] openGLView] addSubview:picker.view];

}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{

    [picker dismissModalViewControllerAnimated:YES];
    [picker.view removeFromSuperview];
    [picker release];
}

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

    newImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    [picker dismissModalViewControllerAnimated:YES];
    [picker.view removeFromSuperview];
    [picker release];



    CCSprite *imageFromPicker = [CCSprite spriteWithCGImage:newImage.CGImage key:@"ImageFromPicker"];

    Background = imageFromPicker;

    int SSw = [CCDirector sharedDirector].winSize.width;
    int SSh = [CCDirector sharedDirector].winSize.height;

    Background.position = ccp(SSw/2, SSh/2);

    Background.scaleX = SSw / Background.textureRect.size.width;
    Background.scaleY = SSh / Background.textureRect.size.height;

    [[self children] removeObjectAtIndex: 0];
    [[self children] insertObject:Background atIndex:0];


}

我该如何解决这个问题?

编辑:

@interface HelloWorldLayer : CCLayer <UIImagePickerControllerDelegate, 

UINavigationControllerDelegate>
{
    CCSprite *infoButton;
    CCSprite *Background;
    UIImage *newImage;
}



-(id) init
{
    if( (self=[super init])) {

        self.isTouchEnabled = true;
        [[CCDirector sharedDirector].openGLView setMultipleTouchEnabled:YES];


        int SSw = [CCDirector sharedDirector].winSize.width;
        int SSh = [CCDirector sharedDirector].winSize.height;

        Background = [CCSprite spriteWithFile:@"B.jpg"];    

        Background.position = ccp(SSw/2, SSh/2);

        Background.scaleX = SSw / Background.textureRect.size.width;
        Background.scaleY = SSh / Background.textureRect.size.height;



        infoButton = [CCSprite spriteWithFile:@"info.png"];
        infoButton.position = ccp(SSw - 15, 15);

        [self addChild:Background];
        [self addChild:infoButton];

        [self schedule:@selector(nextFrame:)];

    }
    return self;
}

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题

检查此行:

CCSprite *imageFromPicker = [CCSprite spriteWithCGImage:newImage.CGImage key:@"ImageFromPicker"];

并尝试将其替换为:

// resize image
UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

// add current shot
CCTexture2D *texture2D = [[[CCTexture2D alloc] initWithImage:image] autorelease]; // this is new
CCSprite *sprite = [CCSprite spriteWithTexture:texture2D]; // this is new

另见: http://www.cocos2d-iphone.org/forum/topic/30259?replies=8#post-149117

希望这有帮助。

答案 1 :(得分:0)

尝试类似的事情:

[Background removeFromParentAndCleanup:YES];

CCSprite *imageFromPicker = [CCSprite spriteWithCGImage:newImage.CGImage key:@"ImageFromPicker"];

Background = imageFromPicker;

int SSw = [CCDirector sharedDirector].winSize.width;
int SSh = [CCDirector sharedDirector].winSize.height;

Background.position = ccp(SSw/2, SSh/2);

Background.scaleX = SSw / Background.textureRect.size.width;
Background.scaleY = SSh / Background.textureRect.size.height;

[[self children] insertObject:Background atIndex:0];