从AVCaptureSession捕获图像

Vad*_*Vad 2 iphone objective-c calayer uiimageview avcapturesession

我正在学习AVCaptureSession以及如何使用其委托方法捕获多个图像

- (void)captureOutput:(AVCaptureOutput *)captureOutput 
     didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 
     fromConnection:(AVCaptureConnection *)connection
Run Code Online (Sandbox Code Playgroud)

我的目标是以每秒预定义的速率捕获1个或多个图像.例如,每1秒1或2个图像.所以我订了

 AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init];
 captureOutput.alwaysDiscardsLateVideoFrames = YES; 
 captureOutput.minFrameDuration = CMTimeMake(1, 1);
Run Code Online (Sandbox Code Playgroud)

当[self.captureSession startRunning];开始我的日志文件显示委托被称为20次.它来自何处以及如何以我预期的间隔捕获图像?

Aje*_*rya 10

您可以使用下面给出的功能,如果要以特定间隔捕获,则设置计时器并再次调用该功能.

-(IBAction)captureNow
    {

    AVCaptureConnection *videoConnection = nil;
    for (AVCaptureConnection *connection in [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: %@", stillImageOutput);
    [stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
    {

        CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
        if (exifAttachments)
        {
            // Do something with the attachments.
            NSLog(@"Attachments: %@", exifAttachments);
        }
        else
        { 
            NSLog(@"No attachments found.");
        }

        NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
        UIImage *image = [[UIImage alloc] initWithData:imageData];
        [[self vImage] setImage:image];

    }];
}
Run Code Online (Sandbox Code Playgroud)

有关更多参考,您可以看到iOS4:使用AVFoundation拍摄带有实时视频预览的照片.