0

我正在制作一个用 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
4

1 回答 1

0

看起来您没有使用自定义Camera类。

你创建一个类

UIImagePickerController* cameraUI = [[UIImagePickerController alloc] init];

设置委托和其他一些项目并将其显示给用户。但这不是Camera类的实例,它是UIImagePickerController. 我相信你想做

Camera* cameraUI = [[Camera alloc] init];

这将为您提供自定义的实施,UIViewController但是还有另一个问题。你的Camera类不是 的子类UIImagePickerController,它是 的子类,UIViewController所以它不会是UIImagePickerController。我想你打算这样做

@interface Camera : UIImagePickerController<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

附带说明一下,命名约定说只有类名应该以大写字母开头。方法名称和变量应以小写字母开头。

从评论编辑:

您的Show方法似乎没有从任何地方调用,所以我不确定它会如何使用。不仅如此,看着方法,我不确定它会如何使用。-开头表示它是一个实例方法,只能在 的实例上调用,Camera但它用于创建和显示的实例,Camera因此该方法需要对象的实例来创建和显示新创建的对象实例. 可能不是预期的。您可以使用 a+使其成为类方法并使用 调用它[Camera Show],但是您将无法访问实例变量,因为它们不存在。

目前,我在任何地方都看不到 aCamera被创建(外部Show)并且可能成为活动视图控制器,因此看起来您只是在显示一个通用UIImagePickerController类而不是您的自定义类。

于 2013-04-02T14:16:17.717 回答