2015-01-22 3 views
11

iOS 맞춤형 카메라 앱에서 Tap-to-Focus를 달성하기 위해이 코드를 사용했지만 작동하지 않습니다. 다음은이 작품 왜 코드iOS 탭으로 초점 맞추기

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { 
    let touchPer = touches.anyObject() as UITouch 
    let screenSize = UIScreen.mainScreen().bounds.size 
    var focus_x = touchPer.locationInView(self.view).x/screenSize.width 
    var focus_y = touchPer.locationInView(self.view).y/screenSize.height 

    if let device = captureDevice { 
     if(device.lockForConfiguration(nil)) { 
      device.focusMode = AVCaptureFocusMode.ContinuousAutoFocus 

      device.focusPointOfInterest = CGPointMake(focus_x, focus_y) 
      device.exposureMode = AVCaptureExposureMode.ContinuousAutoExposure 
      device.unlockForConfiguration() 
     } 
    } 
} 
+1

와 작업 솔루션으로 코디의 대답은 작동하지 않습니다 변환? 어떤 오류가 발생합니까? 예상되는 행동은 무엇입니까? 실제 행동이란 무엇입니까? –

+0

나는 한 지점을 만진다. 그 시점에 초점이 있어야합니다. 그러나 효과가 없습니다. 그 시점에 초점을 맞추고 있지 않습니다. 대신 항상 무한대의 초점에 있습니다. 그것이 문제이다. @David – Krishna

+0

둘 다 취할 수 있도록 보장해 줬습니까? –

답변

4
device.focusPointOfInterest = focusPoint 
device.focusMode = AVCaptureFocusMode.AutoFocus 
device.exposurePointOfInterest = focusPoint 
device.exposureMode = AVCaptureExposureMode.ContinuousAutoExposure 

그렇게하지를하지만 그것은했다.

14
비디오를 표시하는 videoView: UIView

cameraDevice: AVCaptureDevice, 다음이 나를 위해 작동하는 것 같다 : 나는 xy 좌표를 교환하고, 1에서 0으로 x COORD를 매핑했다

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { 
    var touchPoint = touches.first as! UITouch 
    var screenSize = videoView.bounds.size 
    var focusPoint = CGPoint(x: touchPoint.locationInView(videoView).y/screenSize.height, y: 1.0 - touchPoint.locationInView(videoView.x/screenSize.width) 

    if let device = cameraDevice { 
     if(device.lockForConfiguration(nil)) { 
      if device.focusPointOfInterestSupported { 
       device.focusPointOfInterest = focusPoint 
       device.focusMode = AVCaptureFocusMode.AutoFocus 
      } 
      if device.exposurePointOfInterestSupported { 
       device.exposurePointOfInterest = focusPoint 
       device.exposureMode = AVCaptureExposureMode.AutoExpose 
      } 
      device.unlockForConfiguration() 
     } 
    } 
} 

주 왜 0이 아닌 1 - 대신에 왜 그런 경우가 좋을지 모르지만 올바르게 작동하려면 꼭 필요한 것 같습니다. (역시 테스트하기에는 조금 까다 롭습니다.).

편집 : Apple's documentation은 좌표 변환의 이유를 설명합니다.

또한 장치는 관심있는 초점 지점을 지원할 수도 있습니다. focusPointOfInterestSupported를 사용하여 지원 여부를 테스트합니다. 지원되는 경우 focusPointOfInterest를 사용하여 초점을 설정합니다. {0,0}은 그림 영역의 왼쪽 상단을 나타내고 {1,1}은 오른쪽의 홈 버튼이있는 가로 방향 모드의 오른쪽 하단을 나타냅니다.이 장치는 세로 모드에 있더라도 적용됩니다 . 내 예에서

나는 .ContinuousAutoFocus.ContinuousAutoExposure을 사용하고있었습니다,하지만 문서는 .AutoFocus이 올바른 선택을 나타냅니다. 이상하게도 문서에 .AutoExpose에 대한 언급은 없지만 코드에서 사용하고 있습니다.

는 또한 .focusPointOfInterestSupported.exposurePointOfInterestSupported 테스트를 포함하는 내 예제 코드 수정 - 문서는 또한 그것을 설정하기 전에 해당 장치에 사용할 수 있는지 여부를 테스트하기 위해 주어진 초점/노출 모드에 대한 isFocusModeSupported:isExposureModeSupported: 방법을 사용하여 언급을하지만 장치가 관심 지점 모드를 지원하면 자동 모드도 지원한다고 가정합니다. 그것은 모두 내 애플 리케이션에서 잘 작동하는 것 같다.

+0

device.focusMode = AVCptureFocusMode.ContinuousAutoExposure가 잘못되었습니다. "ContinuousAutoFocus"여야합니다 –

+0

죄송합니다, 감사합니다! 그리고 철자법 실수도 2 개! – Cody

+1

AVCaptureVideoPreviewLayer 클래스를 사용하는 경우 captureDevicePointOfInterestForPoint를 사용하는 것이 더 쉽습니다. – Crashalot

0

당신은 올바른 순서의 메소드를 호출 할 필요가 :

if(device.lockForConfiguration(nil)) { 

    device.focusPointOfInterest = CGPointMake(focus_x, focus_y) 
    device.focusMode = AVCaptureFocusMode.ContinuousAutoFocus 

    device.exposureMode = AVCaptureExposureMode.ContinuousAutoExposure 
    device.unlockForConfiguration() 
} 

다른 초점 관심의 이전 시점에 이루어집니다 초점 모드를 설정하기 전에 관심 지점을 설정합니다.

exposurePointOfInterest에도 동일하게 적용됩니다.

3

관심의 초점 포인트를 설정하는 더 좋은 방법 :

let device: AVCaptureDevice! = self.videoDeviceInput!.device 

    do { 
     try device.lockForConfiguration() 

     if device.focusPointOfInterestSupported && device.isFocusModeSupported(focusMode){ 

      device.focusPointOfInterest = devicePoint 
      device.focusMode = focusMode 
     } 

     device.unlockForConfiguration() 

    }catch{ 
     print(error) 
    } 
: 그 관심의 초점 포인트를 설정 한 후

let devicePoint: CGPoint = (self.previewView.layer as! AVCaptureVideoPreviewLayer).captureDevicePointOfInterestForPoint(gestureRecognizer.locationInView(gestureRecognizer.view)) 
  • :

    • 먼저 관심 지점을 계산

  • 2

    스위프트 3.0 솔루션

    스위프트 3. 무엇에 대한

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
        let touchPoint = touches.first! as UITouch 
        let screenSize = cameraView.bounds.size 
        let focusPoint = CGPoint(x: touchPoint.location(in: cameraView).y/screenSize.height, y: 1.0 - touchPoint.location(in: cameraView).x/screenSize.width) 
    
        if let device = captureDevice { 
         do { 
          try device.lockForConfiguration() 
          if device.isFocusPointOfInterestSupported { 
           device.focusPointOfInterest = focusPoint 
           device.focusMode = AVCaptureFocusMode.autoFocus 
          } 
          if device.isExposurePointOfInterestSupported { 
           device.exposurePointOfInterest = focusPoint 
           device.exposureMode = AVCaptureExposureMode.autoExpose 
          } 
          device.unlockForConfiguration() 
    
         } catch { 
          // Handle errors here 
         } 
        } 
    }