2013-07-25 46 views
3

Im使用AVCaptureSession拍照並將照片存儲到相冊。當我點擊按鈕時,它將拍攝快照並存儲到相冊中。但是當我使用橫向模式時,然後單擊它存儲橫向模式的按鈕導致顛倒的靜止圖像。AVCaptureVideoOrientation風景模式導致顛倒的靜止圖像

enter image description here

代碼:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    [self setCaptureSession:[[AVCaptureSession alloc] init]]; 


    [self addVideoInputFrontCamera:NO]; // set to YES for Front Camera, No for Back camera 

    [self addStillImageOutput]; 

    [self setPreviewLayer:[[AVCaptureVideoPreviewLayer alloc] initWithSession:[self captureSession]] ]; 

     [[self previewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill]; 

     CGRect layerRect = [[[self view] layer] bounds]; 


    [[self previewLayer]setBounds:layerRect]; 
    [[self previewLayer] setPosition:CGPointMake(CGRectGetMidX(layerRect),CGRectGetMidY(layerRect))]; 
    [[[self view] layer] addSublayer:[self previewLayer]]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveImageToPhotoAlbum) name:kImageCapturedSuccessfully object:nil]; 


    [[self captureSession] startRunning]; 

camera=[UIButton buttonWithType:UIButtonTypeCustom]; 
[camera setImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal]; 
[camera setFrame:CGRectMake(150, 10, 40, 30)]; 
[camera addTarget:self action:@selector(takephoto:) forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:camera]; 

} 

按鈕拍照:

-(void)takephoto:(id)sender{ 

[self captureStillImage]; 

} 

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

    NSLog(@"about to request a capture from: %@", [self stillImageOutput]); 

    [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection 
                 completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) { 
                  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]; 



                  // [image release]; 
                  [[NSNotificationCenter defaultCenter] postNotificationName:kImageCapturedSuccessfully object:nil]; 

                 }]; 


} 
+0

我建議你閱讀:[爲什麼AVCaptureVideoOrientation橫向模式導致倒掛靜止圖像?](http://stackoverflow.com/questions/7845520/why-does-avcapturevideoorientation-landscape-modes-result- in-upside-down-still-i?rq = 1) – foundry

+0

@HeWas:我已經看到了。但我不知道該怎麼辦? – user2474320

+0

@ user2474320 ...我將整天離線,但如果你今晚沒有有用的答案,我會給你更多的細節 – foundry

回答

6

您需要根據設備的方向來設置videoConnection的videoOrientation財產。在設置AVCaptureConnection之後,在captureStillImage中執行此操作。

UIDeviceOrientation deviceOrientation = 
        [[UIDevice currentDevice] orientation]; 
    AVCaptureVideoOrientation avcaptureOrientation; 
    if (deviceOrientation == UIDeviceOrientationLandscapeLeft) 
      avcaptureOrientation = AVCaptureVideoOrientationLandscapeRight; 

    else if (deviceOrientation == UIDeviceOrientationLandscapeRight) 
      avcaptureOrientation = AVCaptureVideoOrientationLandscapeLeft; 

    [videoConnection setVideoOrientation:avcaptureOrientation]; 
+1

現在讓讀者瞭解爲什麼用戶界面橫向**離開**轉換爲AV捕捉橫向**右鍵**。蘋果充滿了驚喜! – Gui13

+2

這裏有一個提示:設備可以在每邊都有攝像頭。留給後置相機留給右側相機使用? –