Question

I am capturing images from the camera using captureStillImageAsynchronouslyFromConnection

The images are being saved upside down. I tried saving the image using

UIImage *flippedImage = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:UIImageOrientationUp]

but this orientation is only in the metadata and when I copy the data from the file to process it the processed images are upside down. (Quick look shows the originals upright, but the processed/filtered images are upside down).

What is the correct way to set the orientation for the video capturing?

I set the orientation on the video preview layer but that doesn't effect the captured data in the AVCaptureStillImageOutput object's buffer.

I read the AVCaptureStillImageOutput header but there doesn't seem to be an explanation of how to force the camera previews orientation to the image buffer.

e.g. - (AVMetadataObject *)transformedMetadataObjectForMetadataObject:(AVMetadataObject *)metadataObject connection:(AVCaptureConnection *)connection NS_AVAILABLE_IOS(6_0);

Thanks

Was it helpful?

Solution

The "problem" is the image orientation, iOS camera returns images only in one orientation, except if you set the -videoOrientation property that transform already the image taken. So there can be different problems:

  1. videoOrientation non set correctly
  2. videoOrientation not set at ALL

The videoOrientation property works only for still images and video capturing, not for single buffers.
This snippet is tanken from Apple AVCam sample code.

    [[[self stillImageOutput] connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:[[(AVCaptureVideoPreviewLayer *)[[self previewView] layer] connection] videoOrientation]];

    [AVCamViewController setFlashMode:AVCaptureFlashModeAuto forDevice:[[self videoDeviceInput] device]];

    [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:[[self stillImageOutput] connectionWithMediaType:AVMediaTypeVideo] completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {

        if (imageDataSampleBuffer)
        {
            NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
            UIImage *image = [[UIImage alloc] initWithData:imageData];
            [[[ALAssetsLibrary alloc] init] writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:nil];
        }
    }]; <br>

First it sets the orientation according to the preview view, later it takes the shot, and later it saves it on the asset library with correct orientation

OTHER TIPS

As you mentioned, by doing that you are only changing the metadata. You need to use this method to actually flip the image.

Try this:

- (UIImage *)flipImage:(UIImage *)image
{
    UIGraphicsBeginImageContext(image.size);
    CGContextDrawImage(UIGraphicsGetCurrentContext(),CGRectMake(0, 0, image.size.width, image.size.height),image.CGImage);
    UIImage *i = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return i;
}

More info here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top