Change orientation of video or image to portrait with AVCam

268 views Asked by At

I'm using Apple's AVCam source code to create a custom camera, its working like a charm, the problem is once I captured a video or image with it, and then checked it into photo library its orientation gets changed to landscape (even I captured it in portrait orientation). I searched a lot for this, but couldn't find a way for this. Any help?

For a note, my app only supports portrait and capturing should only in portrait.

Update:

AVCaptureConnection *captureConnection = ...
if ([captureConnection isVideoOrientationSupported])
{
    AVCaptureVideoOrientation orientation = AVCaptureVideoOrientationPortrait;
    [captureConnection setVideoOrientation:orientation];
}

This doesn't work.

1

There are 1 answers

0
levo4ka On

For capturing image you should set orientation too. When you save image to disk you should use

writeImageToSavedPhotosAlbum:orientation:completionBlock:

function and set correct "orientation" parameter there too.

Usage: https://developer.apple.com/library/ios/documentation/AssetsLibrary/Reference/ALAssetsLibrary_Class/index.html#//apple_ref/occ/instm/ALAssetsLibrary/writeImageToSavedPhotosAlbum:orientation:completionBlock:

Example on Objective C:

// Flash set to Auto for Still Capture
    [CameraViewController setFlashMode:AVCaptureFlashModeAuto
                             forDevice:[[self videoDeviceInput] device]];

    // Capture a still image.
    [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:[[self stillImageOutput] connectionWithMediaType:AVMediaTypeVideo]
                                                         completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {

        if (imageDataSampleBuffer) {
            self.imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];

            [[[ALAssetsLibrary alloc] init] writeImageToSavedPhotosAlbum:image.CGImage
                                                             orientation:(ALAssetOrientation)[image imageOrientation]
                                                         completionBlock:^(NSURL *assetURL, NSError *error) {
                                                             if(error == nil) {
                                                                 NSLog(@"PHOTO SAVED - assetURL: %@", assetURL);
                                                             } else {
                                                                 NSLog(@"ERROR : %@",error);
                                                             }
                                                         }];