我正在制作一个用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];
}
模态视图控制器按原样打开和关闭,并且代理人肯定会被调用。但是,当游戏轮询状态变量时,它似乎尚未更新,返回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
答案 0 :(得分: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
的实例,因此该方法需要和用于创建和显示新创建的对象实例的对象的实例。可能不是意图。您可以使用+
使其成为类方法并使用[Camera Show]
调用它,但您将无法访问实例变量,因为它们不存在。
目前,我没有看到创建Camera
的任何地方(Show
之外)并且可能成为活动视图控制器,因此看起来您只是显示通用UIImagePickerController
而不是你的自定义类。