UIImagePickerController record video with landscape orientation

怎甘沉沦 提交于 2019-12-03 05:45:13
Maverick

You need to implement a custom UIImagePickerController. For that, you'll need to set imagepicker's property showsCameraControls to FALSE.

self.mImagePickerController.showsCameraControls = NO;
self.mImagePickerController.wantsFullScreenLayout = YES;

Once you do this, no default controls will be visible to you. You'll need to setup custom buttons for start/stop recording and flipping camera etc.

Now, you can use the start/stop methods to record a video:

When one clicks on start recording,

[self.mImagePickerController startVideoCapture];

To stop,

[self.mImagePickerController stopVideoCapture];

To keep track of the switching between the camera, you can use a flag

if (self.mImagePickerController.cameraDevice == UIImagePickerControllerCameraDeviceRear)
    {
        self.mImagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
        isFrontCamera = YES;
    }
    else
    {
        self.mImagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceRear;
        isFrontCamera = NO;
    }

You can set the controls as you wish on the orientation change. AppDelegate.m

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window

This is the delegate that gets called on orientation change. You can use the particular class's object to call its shouldAutorotate method and setup your camera control positions according to the orientation.

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    [self.objController shouldAutorotate];
    return UIInterfaceOrientationMaskAll;
}

Now inside cameraviewcontroller.m

-(BOOL)shouldAutorotate
{
    UIInterfaceOrientation interfaceOrientation = [[UIDevice currentDevice]orientation];

    if(interfaceOrientation == UIInterfaceOrientationPortrait)
    {
        // your controls setup
    }
    else
    {
        // view accordingly for landscape 
    }
}

I have tried to cover all the points. Let me know if you have any confusion. Hope it helps :)

1) You need to check the condition on click event of the flip camera button.

2) AppDelegate.h: Declare an object of your class where you record your video and create a property. Here I'm using CameraViewController as an example.

CameraViewController *objController;

Now in AppDelegate.m:

self.objController = [[CameraViewController alloc]initWithNibName:@"CameraViewController" bundle:nil];

Now use this instance to call the shouldAutorotate method as I have shown above. And return landscape orientation:

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if(isOptionScreenActive == YES)
    {
        [self.shareController shouldAutorotate];
        return UIInterfaceOrientationMaskLandscape;
    }
    else
    {
        [self.anotherController shouldAutorotate];
        return UIInterfaceOrientationMaskPortrait;
    }
}

Here isOptionScreenActive is a flag that is set in the viewWillAppear of the recording class. Set it false in viewDidUnload. Or may be viewWillAppear of another class.

3) I have used cameraviewcontroller.m just as an example. It reflects your recording class. Also in your video recording class's shouldAutorotate method, if the detected orientation is portrait, just return NO. This way the UI wont change and you can keep the UI in landscape only.

try this on may it w'll help you

- (BOOL)shouldAutorotate
{
return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
 return UIInterfaceOrientationLandscapeRight | UIInterfaceOrientationLandscapeLeft;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:    (UIInterfaceOrientation)interfaceOrientation
{
  return (interfaceOrientation == UIInterfaceOrientationLandscapeRight ||       interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

it w'lll work...:)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!