3

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 modes result in upside down still images.

enter image description here

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]] ];

      [[self previewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];

      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];

}

Button for taking picture:

-(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];

                                                         }];


}
user2474320
  • 315
  • 1
  • 5
  • 17
  • I suggest you read: [Why does AVCaptureVideoOrientation landscape modes result in upside down still images?](http://stackoverflow.com/questions/7845520/why-does-avcapturevideoorientation-landscape-modes-result-in-upside-down-still-i?rq=1) – foundry Jul 25 '13 at 07:28
  • @HeWas: I already saw that. But i didn't get idea how to do? – user2474320 Jul 25 '13 at 07:47
  • @user2474320... I will be offline all day but if u have no useful answers by tonight i will give u more detail – foundry Jul 25 '13 at 10:38
  • @HeWas: In portrait mode it working fine. In landscape mode it's not working. Some docs says only use landscape or portrait. But i need to use both mode – user2474320 Jul 25 '13 at 11:24
  • @HeWas: I didn't get answer – user2474320 Jul 29 '13 at 04:26
  • sorry it took a while, I've been busy. I have just posted an answer. For a good standard code sample from Apple, take a look at [SquareCam](https://developer.apple.com/library/ios/#samplecode/SquareCam/Introduction/Intro.html). – foundry Jul 30 '13 at 08:54

1 Answers1

8

You need to set the videoConnection's videoOrientation property based on the orientation of the device. Do this in captureStillImage, after you have set the AVCaptureConnection.

    UIDeviceOrientation deviceOrientation = 
                    [[UIDevice currentDevice] orientation];
    AVCaptureVideoOrientation avcaptureOrientation;
    if ( deviceOrientation == UIDeviceOrientationLandscapeLeft )
            avcaptureOrientation  = AVCaptureVideoOrientationLandscapeRight;

    else if ( deviceOrientation == UIDeviceOrientationLandscapeRight )
            avcaptureOrientation  = AVCaptureVideoOrientationLandscapeLeft;

    [videoConnection setVideoOrientation:avcaptureOrientation];
foundry
  • 31,615
  • 9
  • 90
  • 125
  • 1
    Now it is left to the reader to find out why UI Landscape **left** translates to AVCapture landscape **right**. Apple is full of surprises! – Gui13 Dec 20 '13 at 10:36
  • 2
    Here's a hint: the device can have cameras on each side. Is left for the back camera left for the right camera? – Scott Ahten Apr 22 '16 at 03:06