Search code examples
landscapeimage-rotationios8.1device-orientationgpuimagestillcamera

Issue with orientation of Image captured in landscape mode with GPUImageStillCamera


I am developing an Application where I am capturing image in landscape mode when device orientation is OFF. I am using GPUImageStillCamera for capturing image. But I'm getting issue in rotation of image. Issue is, When I capture image in landscape mode with device rotation OFF and save in gallery, then turning device rotation ON and image must rotate with device orientation. But the image is in landscape when holding device in portrait mode and If holding device in landscape, the image rotation is UpsideDown .

Note

Image rotation works perfect when captured with device rotation ON. Issue is only with image in landscape mode when device rotation OFF. I have tried many solutions like this one. But didn't work for me.

Any help will be appreciated. Thanks


Solution

  • I got solution to this problem . I tried to detect the accelerometer's rotation to get the rotation even when device orientation is OFF. Add CoreMotion.framerwork to your project. Then import CMMotionManager.h in your viewController. In viewDidLoad, add the following code :

    self.motionManager = [[CMMotionManager alloc] init];
        self.motionManager.accelerometerUpdateInterval = 1;
    
        if ([self.motionManager isAccelerometerAvailable])
        {
            NSOperationQueue *queue = [[NSOperationQueue alloc] init];
            [self.motionManager startAccelerometerUpdatesToQueue:queue withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
                dispatch_async(dispatch_get_main_queue(), ^{
    
                    float xx = -accelerometerData.acceleration.x;
                    float yy = accelerometerData.acceleration.y;
                    float angle = atan2(yy, xx);
    
                    // could fire a custom shouldAutorotateToInterfaceOrientation-event here.
                    if(angle >= -2.25 && angle <= -0.75)
                    {
                        if(_deviceOrientation != UIInterfaceOrientationPortrait)
                        {
                            _deviceOrientation = UIInterfaceOrientationPortrait;
                        }
                    }
                    else if(angle >= -0.75 && angle <= 0.75)
                    {
                        if(_deviceOrientation != UIInterfaceOrientationLandscapeRight)
                        {
                            _deviceOrientation = UIInterfaceOrientationLandscapeRight;
                        }
                    }
                    else if(angle >= 0.75 && angle <= 2.25)
                    {
                        if(_deviceOrientation != UIInterfaceOrientationPortraitUpsideDown)
                        {
                            _deviceOrientation = UIInterfaceOrientationPortraitUpsideDown;
                        }
                    }
                    else if(angle <= -2.25 || angle >= 2.25)
                    {
                        if(_deviceOrientation != UIInterfaceOrientationLandscapeLeft)
                        {
                            _deviceOrientation = UIInterfaceOrientationLandscapeLeft;
                        }
                    }
                });
            }];
        } else
            NSLog(@"not active");