2014-04-26 72 views
1

因此,我正在开发一款应用程序,一切都很顺利,但出于某种原因,我似乎无法找到解决此问题的解决方案。该应用程序仅支持肖像模式,但如果我使用水平拍摄手机的图片,则图像右侧朝上放大,并且两侧被截断。我希望它能够让应用程序认为相机总是处于肖像模式,这样水平拍摄的照片就会出现在纵向模式下。有什么想法吗?当在横向拍摄照片时,图像在iOS中显示为肖像

顺便说一句,我正在使用自定义摄像头覆盖。不知道这是否重要,但以防万一。

回答

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解答。

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

这是一个代码片段,你可以根据你的适用性来解决这个问题。

+0

所以我知道,你可以用AVCapture做到这一点,但我使用的UIImagePickerController我试图想出一个办法用它来解决这个问题。我最终创建了一个方法来做到这一点,我会在下面添加我的答案。 – Tony

相关问题