简体   繁体   中英

How can I detect device orientation when the Orientation Lock is on and the app only supports Portrait

The issue that I'm running into is that when a user takes a photo with our app, using AVCaptureSession , I have no way of determining whether they took the photo in Portrait or Landscape mode. Our app only supports Portrait and I keep the Orientation Lock on when using my phone so I'm trying to build a solution assuming that others might do the same.

I looked into using [UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications] but when the Orientation Lock is on, no notifications are ever received. I know that this functionality is possible because the base Camera app and the camera in the Google Hangouts app can detect the rotation (animations on the Cancel and Flash buttons are apparent) when my phone has Orientation Lock on.

Is my best bet to use the accelerometer and detect the angle the phone is being rotated to? An old answer, Detect iPhone screen orientation , makes it very obvious that detecting the angle that way is easy to to do (obviously adapting the answer to use Core Motion instead of UIAccelerometer), but I'm curious if there is another way to do it.

Yes you can do it by looking at the metadata for the image. Don't have time to write up a detailed answer (sorry about that), but I did it for my own project a while back through CMCopyDictionaryOfAttachments(NULL, buffer, kCMAttachmentMode_ShouldPropagate); where I passed in a CMSampleBufferRef for the buffer. I got that buffer from

captureStillImageAsynchronouslyFromConnection:stillImageConnection completionHandler: ^(CMSampleBufferRef imageDataSampleBuffer, NSError *error){} ,

but you can get it from

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection as well.

You can find all the keys for that dictionary here .

Did a quick test with the default camera app with the orientation lock on, and I did get a different orientation for the two pictures. 6 for the portrait one, and 3 for the landscape one.

Again, would love to give you more details about this, but I'm sure you can figure it out by looking through the docs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM