3

사진을 찍고 사진을 앨범에 저장하는 데 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

이제 UI Landscape ** left **가 AVCapture landscape ** right **로 변환되는 이유를 찾아야합니다. Apple은 놀라움으로 가득합니다! – Gui13

+2

여기에 힌트가 있습니다. 장치가 양쪽에 카메라를 가질 수 있습니다. 뒷쪽 카메라가 왼쪽 카메라에 맞게 남았습니까? –