4

AVCaptureSessionを機能させ、Camera.app UIをほぼ完全に複製しましたが、数秒後にアプリケーションがクラッシュし、何が間違っているのかがわかりません。誰かがこれを最適化する方法を知っていることを本当に願っています!

私はARCを使用しています。繰り返しになりますが、セッション全体は正常に実行されますが、少しするとクラッシュします。AVCaptureSessionデリゲートメソッドは、毎秒のように呼び出されます。ユーザーが「写真を撮る」ボタンを押したときにのみそのメソッドを呼び出す方法がある場合、「ライブ」プレビューレイヤーを維持したまま、どうすればそれを行うことができますか?

前もって感謝します!

セッションの設定

NSError *error = nil;
session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetMedium;
device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
[session addInput:input];

output = [[AVCaptureVideoDataOutput alloc] init];
[session addOutput:output];

dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);
[output setSampleBufferDelegate:self queue:queue];
dispatch_release(queue);

output.videoSettings = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey];
if(version >= 4.0 && version < 5.0) {
    output.minFrameDuration = CMTimeMake(1, 15);
}
output.alwaysDiscardsLateVideoFrames = YES;

previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:previewLayer];
[self.view addSubview:camera_overlay];
[session startRunning];

呼び出されているAVCaptureSessionデリゲート:

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
    UIImage *capture_image = [self imageFromSampleBuffer:sampleBuffer];
    return capture_image;
}

サンプルバッファからUIImageを取得するメソッド

- (UIImage *)imageFromSampleBuffer:(CMSampleBufferRef)sampleBuffer {

    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 
    CVPixelBufferLockBaseAddress(imageBuffer, 0); 
    void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer); 
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); 
    size_t width = CVPixelBufferGetWidth(imageBuffer); 
    size_t height = CVPixelBufferGetHeight(imageBuffer); 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst); 
    CGImageRef quartzImage = CGBitmapContextCreateImage(context); 
    CVPixelBufferUnlockBaseAddress(imageBuffer,0);
    CGContextRelease(context); 
    CGColorSpaceRelease(colorSpace);
    UIImage *image = [UIImage imageWithCGImage:quartzImage];
    CGImageRelease(quartzImage);

    return image;

}
4

2 に答える 2

5

完全な例については、AppleのAVCamデモアプリをご覧ください。

方法

- (void)captureOutput:(AVCaptureOutput *)captureOutput 
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 
fromConnection:(AVCaptureConnection *)connection {

カメラフレームの準備ができるたびに呼び出されます。この場合、フレームレートを次のように指定したため、1秒間に15回呼び出されるか、少なくとも15回呼び出される必要があります。output.minFrameDuration = CMTimeMake(1, 15);

あなたが提供したコードから私が考えることができる唯一の理由はあなたがリリースしていないということですUIImage *capture_image

XCode Instrumentsを使用して、アプリケーションのプロファイルを作成し、その理由を確認できます。InstrumentsGuide

リークツールはあなたの場合の最初のストップであり、そのための多くのチュートリアルがWeb上にあります。ここに1つあります。SOユーザーOwenGrossによって作成されたiPhoneメモリリークの追跡です。

于 2011-11-27T05:12:17.623 に答える
0

投稿はかなり古いように見えますが、誰かがこれを見た場合:

デリゲートメソッドで画像を返すのは誰ですか(

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
    UIImage *capture_image = [self imageFromSampleBuffer:sampleBuffer];
    return capture_image;
}

)?

フラグを立てるボタンを使用できます。デリゲートメソッドで、フラグが立てられたかどうかを確認してから、イメージを作成します。画像はインスタンス変数である必要があります。そうでない場合、どのような場合でも失われます。

画像をキャプチャするためのデリゲートメソッドもありますcaptureStillImageAsynchronouslyFromConnection

于 2012-08-28T14:30:55.610 に答える