0

Im using AVCaptureSession for taking pictures and store pictures to album. When i click the button it takes the snapshot and store to album. But when i use landscape mode, then click the button it stores landscape mode image. When preview the image it is in portrait mode.

code:

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

    [self setCaptureSession:[[AVCaptureSession alloc] init]];

    [self  addVideoInputFrontCamera:NO]; // set to YES for Front Camera, No for Back camera

    [self  addStillImageOutput];

    [self setPreviewLayer:[[AVCaptureVideoPreviewLayer alloc] initWithSession:[self captureSession]] ];

    //  UIView *newView = [[UIView alloc] initWithFrame:self.view.bounds];
    // [newView.layer addSublayer: self.previewLayer];
    // [self.view addSubview: newView];

    //previewLayer.frame=self.previewLayer.bounds;

    [[self previewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];

    //  [[self previewLayer] setVideoGravity:AVLayerVideoGravityResize];


  //  self.previewLayer.orientation = AVCaptureVideoOrientationPortrait;

    CGRect layerRect = [[[self view] layer] bounds];

    [[self previewLayer]setBounds:layerRect];
    [[self  previewLayer] setPosition:CGPointMake(CGRectGetMidX(layerRect),CGRectGetMidY(layerRect))];
    [[[self view] layer] addSublayer:[self  previewLayer]];

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveImageToPhotoAlbum) name:kImageCapturedSuccessfully object:nil];

    [[self captureSession] startRunning];

    camera=[UIButton buttonWithType:UIButtonTypeCustom];
    [camera setImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
    [camera setFrame:CGRectMake(150, 10, 40, 30)];
    [camera addTarget:self action:@selector(takephoto:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:camera];
}

capture snapshot:

- (void)takephoto:(id)sender
{
    [self captureStillImage];
}

- (void)captureStillImage
{
    AVCaptureConnection *videoConnection = nil;
    for (AVCaptureConnection *connection in [[self stillImageOutput] connections]) {
        for (AVCaptureInputPort *port in [connection inputPorts]) {
            if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
                videoConnection = connection;
                break;
            }
        }
        if (videoConnection) {
            break;
        }
    }

    NSLog(@"about to request a capture from: %@", [self stillImageOutput]);

    [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection
                                                         completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
                                                             CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
                                                             if (exifAttachments) {
                                                                 NSLog(@"attachements: %@", exifAttachments);
                                                             } else {
                                                                 NSLog(@"no attachments");
                                                             }

                                                             NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
                                                             UIImage *image = [[UIImage alloc] initWithData:imageData];


                                                            [self setStillImage:image];



                                                             //  [image release];
                                                             [[NSNotificationCenter defaultCenter] postNotificationName:kImageCapturedSuccessfully object:nil];

                                                         }];
}

I'm using self.previewLayer.orientation = UIInterfaceOrientationLandscapeLeft; for landscape mode

Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
user2474320
  • 315
  • 1
  • 5
  • 17

1 Answers1

1

In addition to previewLayer orientation you should set the videoOrientation for correcting this issue.

AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in [[self stillImageOutput] connections]) {
    for (AVCaptureInputPort *port in [connection inputPorts]) {
        if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
            videoConnection = connection;
            break;
        }
    }
    if (videoConnection) {
        break;
    }
}

after this

[videoConnection setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft];

set the orientation by checking weather app in landscape or portrait mode..

user3608500
  • 835
  • 4
  • 10