2011-10-27 5 views
4

をフリーズ私はiOSの4以来、焦点のポイントを設定するには、以下の方法を使用しています:iOSの4デバイスでのiOS 5 - AVCaptureDeviceは、フォーカスポイントを設定し、フォーカスモードがライブカメラ映像

- (void) focusAtPoint:(CGPoint)point 
{ 
    AVCaptureDevice *device = [[self captureInput] device]; 

    NSError *error; 

    if ([device isFocusModeSupported:AVCaptureFocusModeAutoFocus] && 
     [device isFocusPointOfInterestSupported]) 
    { 
     if ([device lockForConfiguration:&error]) { 
      [device setFocusPointOfInterest:point]; 
      [device setFocusMode:AVCaptureFocusModeAutoFocus]; 
      [device unlockForConfiguration]; 
     } else { 
      NSLog(@"Error: %@", error); 
     } 
    } 
} 

を、これは何の問題もなく動作します。しかし、iOS 5では、ライブカメラのフィードがフリーズし、数秒後に完全に黒くなります。例外もエラーもスローされません。

setFocusPointOfInterestまたはsetFocusModeのいずれかをコメントアウトするとエラーは発生しません。したがって、これらの組み合わせによって、この動作につながります。

+0

iOS 5で全く同じコードを使用しても問題はありません。あなたのエラーは他の場所にありますか? – diatrevolo

+0

重複:http://stackoverflow.com/questions/8065422/iphone-camera-focussing?rq=1 – nicc

答えて

8

setFocusPointOfInterest:を指定したポイントが正しくありません。それがクラッシュする理由です。

は、あなたのプログラムにこのメソッドを追加し、私は@Louisの答えにいくつかの追加情報を提供したい、この機能によって

- (CGPoint)convertToPointOfInterestFromViewCoordinates:(CGPoint)viewCoordinates 
{ 
    CGPoint pointOfInterest = CGPointMake(.5f, .5f); 
    CGSize frameSize = [[self videoPreviewView] frame].size; 

    AVCaptureVideoPreviewLayer *videoPreviewLayer = [self prevLayer]; 

    if ([[self prevLayer] isMirrored]) { 
     viewCoordinates.x = frameSize.width - viewCoordinates.x; 
    }  

    if ([[videoPreviewLayer videoGravity] isEqualToString:AVLayerVideoGravityResize]) { 
     pointOfInterest = CGPointMake(viewCoordinates.y/frameSize.height, 1.f - (viewCoordinates.x/frameSize.width)); 
    } else { 
     CGRect cleanAperture; 
     for (AVCaptureInputPort *port in [[[[self captureSession] inputs] lastObject] ports]) { 
      if ([port mediaType] == AVMediaTypeVideo) { 
       cleanAperture = CMVideoFormatDescriptionGetCleanAperture([port formatDescription], YES); 
       CGSize apertureSize = cleanAperture.size; 
       CGPoint point = viewCoordinates; 

       CGFloat apertureRatio = apertureSize.height/apertureSize.width; 
       CGFloat viewRatio = frameSize.width/frameSize.height; 
       CGFloat xc = .5f; 
       CGFloat yc = .5f; 

       if ([[videoPreviewLayer videoGravity] isEqualToString:AVLayerVideoGravityResizeAspect]) { 
        if (viewRatio > apertureRatio) { 
         CGFloat y2 = frameSize.height; 
         CGFloat x2 = frameSize.height * apertureRatio; 
         CGFloat x1 = frameSize.width; 
         CGFloat blackBar = (x1 - x2)/2; 
         if (point.x >= blackBar && point.x <= blackBar + x2) { 
          xc = point.y/y2; 
          yc = 1.f - ((point.x - blackBar)/x2); 
         } 
        } else { 
         CGFloat y2 = frameSize.width/apertureRatio; 
         CGFloat y1 = frameSize.height; 
         CGFloat x2 = frameSize.width; 
         CGFloat blackBar = (y1 - y2)/2; 
         if (point.y >= blackBar && point.y <= blackBar + y2) { 
          xc = ((point.y - blackBar)/y2); 
          yc = 1.f - (point.x/x2); 
         } 
        } 
       } else if ([[videoPreviewLayer videoGravity] isEqualToString:AVLayerVideoGravityResizeAspectFill]) { 
        if (viewRatio > apertureRatio) { 
         CGFloat y2 = apertureSize.width * (frameSize.width/apertureSize.height); 
         xc = (point.y + ((y2 - frameSize.height)/2.f))/y2; 
         yc = (frameSize.width - point.x)/frameSize.width; 
        } else { 
         CGFloat x2 = apertureSize.height * (frameSize.height/apertureSize.width); 
         yc = 1.f - ((point.x + ((x2 - frameSize.width)/2))/x2); 
         xc = point.y/frameSize.height; 
        } 

       } 

       pointOfInterest = CGPointMake(xc, yc); 
       break; 
      } 
     } 
    } 

    return pointOfInterest; 
} 
+1

私はまだこの機能をテストしていませんが、いくつかの同様のものを実装しています。そして、あなたは私が持っていない余分なマイルを過ごしたようです、重力のあらゆる種類の会計のために。いい仕事です! –

+3

このコードは、Apple AVCamのデモから来ています。https://developer.apple.com/library/ios/#samplecode/AVCam/Introduction/Intro.html – jverdi

+0

アップルの最新APIでは、これをすべて1行で行うことはできませんコードの? – Senseful

1

を返された値を使用します。

Apple's documentsによると(太字部分に注意してください):

また、デバイスは、関心のフォーカスポイントをサポートすることができます。 focusPointOfInterestSupportedを使用してサポートをテストします。サポートされている場合は、focusPointOfInterestを使用してフォーカルポイントを設定します。 {0,0}はピクチャエリアの左上を表し、{1,1}は右にホームボタンを持つ横モードの右下を表します。これは、デバイスがポートレートにあっても適用されますモード。

FocusPointOfInterestを計算する際には方向を含める必要があります。

関連する問題