8

私のアプリケーションでは、 AVCaptureVideoPreviewLayer を表示し、ユーザーがAVCaptureOutputのcaptureStillImageAsynchronouslyFromConnection関数を使用してボタンをクリックすると静止画像をキャプチャしています。これは、iPhone 5 まではうまく機能していましたが、iPhone 5 では完全には機能しませんでした。

私のセットアップコードは次のとおりです。

...
self.imageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
[self.imageOutput setOutputSettings:outputSettings];

self.captureSession = [[[AVCaptureSession alloc] init] autorelease];
[self.captureSession addInput:self.rearFacingDeviceInput];
[self.captureSession addOutput:self.imageOutput];
[self.captureSession setSessionPreset:AVCaptureSessionPresetPhoto];

self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
self.previewLayer.frame = CGRectMake(0, 0, 320, 427);
self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspect;

[self.captureSession startRunning];
[outputSettings release];

私のキャプチャ方法は次のとおりです。

AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in self.imageOutput.connections){
    for (AVCaptureInputPort *port in [connection inputPorts]){
        if ([[port mediaType] isEqual:AVMediaTypeVideo] ){
            videoConnection = connection;
            break;
        }
    }
    if (videoConnection) { break; }
}

//code to abort if not return 'soon'
...

[self.imageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error){

    //use image here

}];

iPhone5を使用している場合、captureStillImageAsynchronouslyFromConnectionが完了しません

私はテストしました:

  • このコードは、更新された iPhone 4s と iPod (iPod touch (第 4 世代)) の両方で動作するため、OS 6 ではありません。

  • captureSession が実行中です

  • videoConnection は nil ではありません

  • imageOutput は nil ではありません

また:

  • プレビューをサブビューとして配置する必要があるため、UIImagePickerController ではなくこのメソッドを使用しています。

  • キャプチャ セッションでstopRunningを呼び出すと、iPhone 5 でも数秒かかります。

4

1 に答える 1

0

さて、このコードは正常に動作します。iPhone 4 と 5 の両方でテスト済み (baseSDK 7.1、ARC の下)。

考慮しなければならないことはほとんどありません。

1)rearFacingDeviceInputを正しく設定してください。

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[self setRearFacingDeviceInput:[AVCaptureDeviceInput deviceInputWithDevice:device error:nil]];

2) Vincentが述べたように、エラーが発生します。エラーと imageSampleBuffer の両方をログに記録してみてください。

3) セッションの -startRunning および -stopRunning 操作は完了するまでに長い時間がかかります (数秒、場合によっては 5 ~ 6 秒)。スレッド、1つの方法はGCDを使用しています

dispatch_queue_t serialQueue = dispatch_queue_create("queue", NULL);
dispatch_async(serialQueue, ^{
    [self.captureSession startRunning];
});

それでも captureStillImageAsynchronously が完了しない場合 (確実にブロックにブレークポイントを追加し、すべてをログに記録します)、デバイスのカメラを確認する必要があります。あなたのコードはすべての iPhone 5 デバイスで動作すると思います。これが役に立てば幸いです。幸運を祈ります。

于 2014-05-06T08:37:20.080 に答える