在横向拍摄照片时,图像在iOS中以纵向显示

时间:2014-04-26 09:13:40

标签: ios objective-c ios7 uiimage uiimagepickercontroller

所以我正在开发一个应用程序,一切顺利,但出于某种原因,我似乎无法找到解决这个问题的方法。该应用程序仅支持纵向模式,但如果我用手机水平拍照,则图像会正面朝上,放大,并且两侧会被截断。我希望应用程序认为相机始终处于纵向模式,这样水平拍摄的照片就会像纵向模式一样侧向显示。有什么想法吗?

顺便说一句,我使用的是自定义相机叠加层。不确定这是否重要,但以防万一。

2 个答案:

答案 0 :(得分:0)

您可以使用 AVCaptureSession 拍摄静止图像。试试这个

- (void)addVideoInputFromCamera
{
    AVCaptureDevice *backCamera;

    NSArray *devices = [AVCaptureDevice devices];

    for (AVCaptureDevice *device in devices)
    {
        if ([device hasMediaType:AVMediaTypeVideo])
        {
            if ([device position] == AVCaptureDevicePositionBack)
            {
                backCamera = device;
                [self toggleFlash];
            }
        }
    }

    NSError *error = nil;

    AVCaptureDeviceInput *backFacingCameraDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:backCamera error:&error];

    if (!error)
    {
        if ([_captureSession canAddInput:backFacingCameraDeviceInput])
        {
            [_captureSession addInput:backFacingCameraDeviceInput];
        }
    }
}

- (void)addStillImageOutput
{
    [self setStillImageOutput:[[AVCaptureStillImageOutput alloc] init]];
    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey,nil];
    [[self stillImageOutput] setOutputSettings:outputSettings];

    AVCaptureConnection *videoConnection = nil;

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

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

    [_stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection
                                                         completionHandler:
     ^(CMSampleBufferRef imageSampleBuffer, NSError *error) {

         if (imageSampleBuffer)
         {
             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];

             [[NSNotificationCenter defaultCenter] postNotificationName:kImageCapturedSuccessfully object:nil];
         }
     }];
}

这是一个代码段,您可以根据自己的适用性进行解决。

答案 1 :(得分:0)

所以这张照片似乎是在风景中拍摄的,我只是想让它在画像中侧身出现。我想如果图像实际上是在风景中,那么它看起来是拉伸和截断的,因为它的宽度大于它的高度。所以我以下面的方式使用它作为if条件,它对我有用。

 UIImage *imageAsTaken = [info objectForKey:UIImagePickerControllerOriginalImage];

    if (imageAsTaken.size.width > imageAsTaken.size.height) {
        UIImage *imagePortrait = [UIImage imageWithCGImage:imageAsTaken.CGImage scale:imageAsTaken.scale orientation:UIImageOrientationRight];
        self.image = imagePortrait;
        [self.imageView setContentMode:UIViewContentModeScaleAspectFill];
        self.imageView.image = self.image;
        NSLog(@"landscape");
    }
    else {
        self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
        [self.imageView setContentMode:UIViewContentModeScaleAspectFill];
        self.imageView.image = self.image;
        NSLog(@"portrait");
    }

现在这可能不是"官方"或"对"解决它的方法,但它是有道理的,它的工作原理和读这个的任何人都可以随意使用这个解决方案。感谢Krishna Kumar对AVCapture的回答。