我正在制作一个用 C++ 制作的游戏,它在某些时候想要打开使用相机。为此,它与一个提供模态视图控制器的 Objective C 类接口:
UIImagePickerController* cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraUI.mediaTypes = [[NSArray alloc] initWithObjects:(NSString*)kUTTypeImage, nil];
cameraUI.allowsEditing = YES;
cameraUI.delegate = self;
[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentModalViewController:cameraUI animated:YES];
此类公开一个 State 变量以允许游戏观察模态视图控制器的进度。它被初始化:
State = CAMERA_ACTIVITY_WORKING;
并由 UIImageControllerDelegate 函数更新:
- (void)imagePickerControllerDidCancel: (UIImagePickerController*) picker
{
State = CAMERA_ACTIVITY_CANCELED;
[[[[UIApplication sharedApplication] keyWindow] rootViewController] dismissModalViewControllerAnimated:YES];
[picker release];
}
- (void)imagePickerController: (UIImagePickerController*) picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
State = CAMERA_ACTIVITY_IMAGECAPTURED;
[[[[UIApplication sharedApplication] keyWindow] rootViewController] dismissModalViewControllerAnimated:YES];
[picker release];
}
模态视图控制器按应有的方式打开和关闭,并且代表肯定会被调用。但是,当游戏轮询 State 变量时,它似乎没有更新并返回 CAMERA_ACTIVITY_WORKING。我曾尝试使变量 volatile 但这没有效果。
有人可以帮忙吗?
编辑:全类源代码
。H
@interface Camera : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
volatile State State;
UIImage* CapturedImage;
}
- (void)imagePickerControllerDidCancel: (UIImagePickerController*) picker;
- (void)imagePickerController: (UIImagePickerController*) picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
- (bool) IsAvailable;
- (bool) Show;
- (State) GetState;
@end
.m
@implementation Camera
- (id)init
{
self = [super init];
if(!self) return self;
State = CAMERA_ACTIVITY_WORKING;
CapturedImage = NULL;
return self;
}
- (void)imagePickerControllerDidCancel: (UIImagePickerController*) picker
{
DEBUG_LOG("imagePickerControllerDidCancel");
State = Poppet::ICameraActivity::CAMERA_ACTIVITY_CANCELED;
DEBUG_LOG("State: " + STRING_CAST(State));
[[[[UIApplication sharedApplication] keyWindow] rootViewController] dismissModalViewControllerAnimated:YES];
[picker release];
}
- (void)imagePickerController: (UIImagePickerController*) picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
State = CAMERA_ACTIVITY_IMAGECAPTURED;
[[[[UIApplication sharedApplication] keyWindow] rootViewController] dismissModalViewControllerAnimated:YES];
[picker release];
}
- (bool) IsAvailable
{
return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == YES;
}
- (bool) Show
{
if(![self IsAvailable]) return false;
State = CAMERA_ACTIVITY_WORKING;
UIImagePickerController* cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera; //Get Image From Camera
cameraUI.mediaTypes = [[NSArray alloc] initWithObjects:(NSString*)kUTTypeImage, nil];
cameraUI.allowsEditing = YES;
cameraUI.delegate = self;
[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentModalViewController:cameraUI animated:YES];
return true;
}
- (State) GetState
{ return State; }
@end