【问题标题】:writeImageToSavedPhotosAlbum save only a few imageswriteImageToSavedPhotosAlbum 只保存几张图片
【发布时间】:2011-07-21 14:51:57
【问题描述】:

我一直在关注苹果示例QA1702,了解如何使用 AVFoundation 捕获图像。由于篇幅问题,我不会在这里引用代码。简要说明我要实现的目标: 使用 iPhone 相机将“视频”(实际上是一系列图像)传递到网络服务器,我知道这是可能的。但是,为了能够像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,因为我在此处使用 CGImageRef 执行 writeImageToSavedPhotosAlbum:。

我看到的问题是,在我捕获图像的 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: 永远不会被调用?有任何想法吗?

【问题讨论】:

    标签: iphone objective-c camera assetslibrary


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-21
      • 2020-04-21
      • 2022-10-17
      • 1970-01-01
      • 2016-01-06
      • 1970-01-01
      • 1970-01-01
      • 2015-04-14
      相关资源
      最近更新 更多