Pergunta

So I'm developing an app and everything's going well, but for some reason I can't seem to find a solution to this problem. The app only supports portrait mode, but if I take a picture with the phone horizontally then the image appears right-side up, zoomed in, and the sides are truncated. I'd like it to be so that the app thinks that the camera is always in portrait mode, that way a picture taken horizontally will just appear like it's sideways in portrait mode. Any thoughts?

By the way, I'm using a custom camera overlay. Not sure if that matters, but just in case.

Foi útil?

Solução 2

So the picture appeared to have been taken in landscape, and I simply wanted it to appear sideways in portrait. I figured that if the image was actually in landscape, then it was appearing stretched and truncated because it's width was larger than its height. So I used this as an if condition in the following way, and it worked for me.

 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");
    }

Now this may not be the "official" or "right" way to solve it, but it makes sense and it works and anyone reading this can feel free to use this solution. Thanks to Krishna Kumar for the AVCapture answer as well.

Outras dicas

You can use AVCaptureSession to take still images. Try this

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

This is a code snippet you can workaround according to your suitability.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top