AVFoundationを使用して画像をキャプチャする方法について、リンゴの例QA1702に従っています。スペースの都合上、ここではコードを引用しません。私が達成しようとしていることの簡単な説明: iPhone カメラを使用して、「ビデオ」(実際には画像のシーケンス) を Web サーバーに渡します。これが可能であることはわかっています。ただし、この例のように HTTP POST を使用して画像を渡すことができるようにするには、画像を保存する必要があります。必ずしも写真アルバムにあるとは限りませんが、デバッグ目的で写真を表示したくありません。
Apple 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 の作成を削除し、代わりに CGImageRef を使用して writeImageToSavedPhotosAlbum: を実行するため、void typ に変更しました。
私が見ている問題は、画像をキャプチャする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: が呼び出されることはありませんか? 何か案は?