writeImageToSavedPhotosAlbum只保存几张图片

时间:2011-07-21 14:51:58

标签: iphone objective-c camera assetslibrary

我一直在关注如何使用AVFoundation捕获图像的苹果示例QA1702。由于空间问题,我不会在这里引用代码。我正在努力实现的简要描述: 使用iPhone相机将“视频”(实际上是一系列图像)传递给Web服务器,我知道这是可能的。但是,为了能够像this example中那样使用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,因为我执行了writeImageToSavedPhotosAlbum:此处使用CGImageRef。

我看到的问题是,在10秒内我捕获图像~150次调用captureOutput,因此创建了相同数量的writeImageToSavedPhotos,但只保存了~5-10张图片。我知道记忆滥用这是因为我没有得到任何警告我无法弄清楚为什么不创建更多的图像。我能做些什么呢?是因为,我现在只是猜测,writeImageToSavedPhotos开始新线程,iPhone无法处理超过一定数量的线程。我已经阅读了有关NSOperationQueue的内容,我应该调查一下吗?

另外,我在setupCaptureSession中使用了NSTimer:

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

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

1 个答案:

答案 0 :(得分:0)

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

相关问题