0

我一直在关注苹果示例QA1702,了解如何使用 AVFoundation 捕获图像。由于篇幅问题,我不会在这里引用代码。我要实现的目标的简要描述:使用 iPhone 摄像头将“视频”(实际上是一系列图像)传递到 Web 服务器,我知道这是可能的。但是,为了能够像本例中那样使用 HTTP POST 传递图像,我必须保存图像。不一定在相册中,但出于调试目的,我也无法查看那里的图片。

苹果QA1702包含3种方法:

- (void)setupCaptureSession 

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

//this is modified to be void as you might see, will get back to this
- (void) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer

在 setupCaptureSession 中,我按照示例启动会话。captureOutput 仅运行 imageFromSampleBuffer,这就是我添加一些更改的地方:

// Create a Quartz image from the pixel data in the bitmap graphics context
CGImageRef quartzImage = CGBitmapContextCreateImage(context); 
// Unlock the pixel buffer
CVPixelBufferUnlockBaseAddress(imageBuffer,0);

// Free up the context and color space
CGContextRelease(context); 
CGColorSpaceRelease(colorSpace);

//library is declared in .h and is a ALAssetsLibrary
[library writeImageToSavedPhotosAlbum:quartzImage orientation:ALAssetOrientationDown completionBlock:nil];

// Release the Quartz image
CGImageRelease(quartzImage); 

我已经删除了 UIImage 的创建并将其更改为 void typ,因为我在此处使用 CGImageRef 执行 writeImageToSavedPhotosAlbum:。

我看到的问题是,在我捕获图像的 10 秒内,对 captureOutput 进行了约 150 次调用,因此 writeImageToSavedPhotos 的数量相同,但只保存了约 5-10 张图片。我知道这是内存滥用,但由于我没有收到任何警告,我无法弄清楚为什么不创建更多图像。我该怎么办?是不是因为,我现在只是在猜测,writeImageToSavedPhotos 启动了新线程,而 iPhone 不能处理超过一定数量的线程。我读过一些关于 NSOperationQueue 的内容,我应该看看吗?

附带说明一下,我在 setupCaptureSession 中使用了 NSTimer:

    [NSTimer scheduledTimerWithTimeInterval: 10.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats: NO];

但是我想在第一次调用 captureOutput 时启动它,以避免在摄像机启动期间经过时间。但是如果我将此代码行移动到 captureOutput 那么 timerFireMethod: 永远不会被调用?有任何想法吗?

4

1 回答 1

0

这可以通过 NSOperationQueue 解决,但对我来说不再感兴趣,因为写入文件对于大多数应用程序来说是无效的。

于 2011-08-12T12:21:12.233 回答