我的cocos2d项目中有2个场景。在我的第一个场景中,我有一个按钮,弹出UIImagePicker控制器,允许我从相机中选择和成像。 我通过将我的UIImage转换为NSData将图像存储到NSUserDefaults中。 请参阅以下代码
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
// Access the uncropped image from info dictionary
[myController dismissModalViewControllerAnimated:YES];
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
CGRect myRect = CGRectMake(image.size.width/2-160, image.size.height/2-160, 320, 320);
UIImage *resultImage = [self cropUIImage:image atRect:myRect];
NSData *imageData;
imageData = [NSKeyedArchiver archivedDataWithRootObject:resultImage];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@"camera" forKey:@"pictureSource"];
[defaults removeObjectForKey:@"picId"];
[defaults setObject:imageData forKey:@"picId"];
[defaults synchronize];
[picker release];
//move to screen
[[CCDirector sharedDirector] replaceScene: [CCTransitionSlideInR transitionWithDuration:0.5f scene:[cameraImageScene node]]];
}
然后我将屏幕更换为相机图像屏幕,我在那里使用精灵显示图像。
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *imageData;
imageData = [defaults objectForKey:@"picId"];
UIImage *imageU = [NSKeyedUnarchiver unarchiveObjectWithData: imageData];
CCSprite *image = [CCSprite spriteWithCGImage:[imageU CGImage] key:@"cam"];
image.scale = 0.75f;
[image setPosition:ccp(winSize.width/2-3,287)];
[self addChild:image z:2];
在这个图层中,我有一个后退按钮,可以将用户带回第一个场景。 当我第一次从画廊中选择图像时,一切正常。但当我触摸然后选择另一个图像时,旧的选择显示为精灵中的图像。
任何提示?