jeudi 29 janvier 2015

AV Camera is not rotating correctly

I am working on an application which uses the frontcamera to display the user. I am using the following code:


I have the following problem. Can you please help me? My view which is containing the camerapreview does not rotoate correctly. I like the app to only work in landscape mode but the cameraorientation is not working(see screenshot)! You can see the device is in landscapemode but my neck is on the right and the fishes are swimming vertically ;) first pic Holding the device in portrait the camera is working: Second Screenshot; Device in Portraitmode



// CameraController.m

#import "CameraController.h"
#import <AVFoundation/AVFoundation.h>

@interface CameraController ()

@end

@implementation CameraController
AVCaptureSession *session;
AVCaptureStillImageOutput *stillImageOutput;

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
session = [[AVCaptureSession alloc] init];
[session setSessionPreset:AVCaptureSessionPresetPhoto];
AVCaptureDevice *inputDevice = [self frontCamera];

NSError *error;
AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&error];

if([session canAddInput:deviceInput]){
[session addInput:deviceInput];
}

AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
CALayer *rootLayer = [[self view] layer];
[rootLayer setMasksToBounds:YES];
CGRect frame = self.frameForCapture.frame; //link to UI

[previewLayer setFrame:frame];

[rootLayer insertSublayer:previewLayer atIndex:0];

stillImageOutput = [[AVCaptureStillImageOutput alloc] init]; //when the photo gets taken
NSDictionary *outputSettings = [[NSDictionary alloc]
initWithObjectsAndKeys:AVVideoCodecJPEG, AVVideoCodecKey, nil];

[stillImageOutput setOutputSettings:outputSettings];
[session addOutput:stillImageOutput];
[session startRunning]; //starts the live preview
}

/*....*/
- (AVCaptureDevice *)frontCamera {
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices) {
if ([device position] == AVCaptureDevicePositionFront) {
return device;
}
}
return nil;
}
@end



Aucun commentaire:

Enregistrer un commentaire