2014-04-26 99 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

相關問題