gaurav
gaurav

Reputation: 347

How to use camera view as background in inner layer only

I want to use camera as my game background it is working fine when i put the code in appDelegate.m but when i start the game it is showing on everypage when i go through it is appearing for a sec and then background cover the same but i want to show only on one screen that is my game screen not menu screen.when i put the code on that layer file it is not working.I am using the below code for that:

Add this in appDelegate.h

UIView *overlay;

Add this in appDelegate.m

EAGLView *glView = [EAGLView viewWithFrame:[window bounds] 
    pixelFormat:kEAGLColorFormatRGBA8 depthFormat:0];

Below the [window addSubview: viewController.view]; line of code we will add the following lines of code:
// set the background color of the view
[CCDirector sharedDirector].openGLView.backgroundColor = [UIColor clearColor];
[CCDirector sharedDirector].openGLView.opaque = NO;

// set value for glClearColor
glClearColor(0.0, 0.0, 0.0, 0.0);

// prepare the overlay view and add it to the window
overlay = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
overlay.opaque = NO;
overlay.backgroundColor=[UIColor clearColor];
[window addSubview:overlay];

Next add the following code below the code you just added.

#define CAMERA_TRANSFORM  1.24299

UIImagePickerController *uip;

@try {
    uip = [[[UIImagePickerController alloc] init] autorelease];
    uip.sourceType = UIImagePickerControllerSourceTypeCamera;
    uip.showsCameraControls = NO;
    uip.toolbarHidden = YES;
    uip.navigationBarHidden = YES;
    uip.wantsFullScreenLayout = YES;
    uip.cameraViewTransform = CGAffineTransformScale(uip.cameraViewTransform, 
        CAMERA_TRANSFORM, CAMERA_TRANSFORM);
}
@catch (NSException * e) {
    [uip release];
    uip = nil;
}
@finally {
    if(uip) {
        [overlay addSubview:[uip view]];
        [overlay release];
    }
}


[window bringSubviewToFront:viewController.view];

Please help to find out where i can put this code in xxxxxxxx.m so it will work for only xxxxxxxx.m file.

Thanks, Gaurav

Upvotes: 1

Views: 1045

Answers (1)

CodeSmile
CodeSmile

Reputation: 64477

Make overlay a property of the app delegate class.

Access the overlay view from anywhere:

YourAppDelegate* appDelegate = (YourAppDelegate*)
                                  [[UIApplication sharedApplication] delegate];
UIView* overlay = appDelegate.overlay;
// add the UIImagePickerController code here …

Whenever you want to disable the camera view, hide the overlay view:

overlay.hidden = YES;

Upvotes: 1

Related Questions