2011-03-04 63 views
3

我使用下面的代碼來創建一個視頻從一系列圖像存儲到一個數組(takenImages)。持續時間以秒爲單位的幀持續時間。avfoundation:appendPixelBuffer withPresentationTime,適配器沒有準備好

然後,所有東西都保存到庫中。

我的問題是,如果我與調試器中運行,該視頻是完全保存,但如果我沒有調試器中運行視頻並非完全保存(只是第一幀被保存)。

int timescale = duration * [takenImages count]; 

    for (int i = 0; i < [takenImages count]; i++) { 
     CVPixelBufferRef buffer = NULL; 

     buffer = [self pixelBufferFromCGImage:[[takenImages objectAtIndex:i] CGImage]]; 

     BOOL append_ok = NO; 
     int j = 0; 
     while (!append_ok && j < 30) { 
      if (adaptor.assetWriterInput.readyForMoreMediaData) { 
       printf("appending %d attemp %d\n", i, j); 
       append_ok = [adaptor appendPixelBuffer:buffer withPresentationTime:CMTimeMake(i*duration,timescale)]; 
      } else { 
       printf("adaptor not ready %d, %d\n", i, j); 
       [NSThread sleepForTimeInterval:0.1]; 
      } 
      j++; 
     } 

     if (!append_ok) { 
      printf("error appending image %d times %d\n", i, j); 
     } 

    } 

這是我在控制檯中運行(調試器關)之後的示例:

appending 0 attemp 0 
appending 1 attemp 0 
adaptor not ready 2, 0 
adaptor not ready 2, 1 
adaptor not ready 2, 2 
adaptor not ready 2, 3 
adaptor not ready 2, 4 
adaptor not ready 2, 5 
adaptor not ready 2, 6 
adaptor not ready 2, 7 
adaptor not ready 2, 8 
adaptor not ready 2, 9 
adaptor not ready 2, 10 
adaptor not ready 2, 11 
adaptor not ready 2, 12 
adaptor not ready 2, 13 
adaptor not ready 2, 14 
adaptor not ready 2, 15 
adaptor not ready 2, 16 
adaptor not ready 2, 17 
adaptor not ready 2, 18 
adaptor not ready 2, 19 
adaptor not ready 2, 20 
adaptor not ready 2, 21 
adaptor not ready 2, 22 
adaptor not ready 2, 23 
adaptor not ready 2, 24 
adaptor not ready 2, 25 
adaptor not ready 2, 26 
adaptor not ready 2, 27 
adaptor not ready 2, 28 
adaptor not ready 2, 29 
error appending image 2 times 30 
adaptor not ready 3, 0 
adaptor not ready 3, 1 
adaptor not ready 3, 2 
adaptor not ready 3, 3 
adaptor not ready 3, 4 
adaptor not ready 3, 5 
adaptor not ready 3, 6 
adaptor not ready 3, 7 
adaptor not ready 3, 8 
adaptor not ready 3, 9 
adaptor not ready 3, 10 
adaptor not ready 3, 11 
adaptor not ready 3, 12 
adaptor not ready 3, 13 
adaptor not ready 3, 14 
adaptor not ready 3, 15 
adaptor not ready 3, 16 
adaptor not ready 3, 17 
adaptor not ready 3, 18 
adaptor not ready 3, 19 
adaptor not ready 3, 20 
adaptor not ready 3, 21 
adaptor not ready 3, 22 
adaptor not ready 3, 23 
adaptor not ready 3, 24 
adaptor not ready 3, 25 
adaptor not ready 3, 26 
adaptor not ready 3, 27 
adaptor not ready 3, 28 
adaptor not ready 3, 29 
error appending image 3 times 30 
adaptor not ready 4, 0 
adaptor not ready 4, 1 
adaptor not ready 4, 2 
adaptor not ready 4, 3 
adaptor not ready 4, 4 
adaptor not ready 4, 5 
adaptor not ready 4, 6 
adaptor not ready 4, 7 
adaptor not ready 4, 8 
adaptor not ready 4, 9 
adaptor not ready 4, 10 
adaptor not ready 4, 11 
adaptor not ready 4, 12 
adaptor not ready 4, 13 
adaptor not ready 4, 14 
adaptor not ready 4, 15 
adaptor not ready 4, 16 
adaptor not ready 4, 17 
adaptor not ready 4, 18 
adaptor not ready 4, 19 
adaptor not ready 4, 20 
adaptor not ready 4, 21 
adaptor not ready 4, 22 
adaptor not ready 4, 23 
adaptor not ready 4, 24 
adaptor not ready 4, 25 
adaptor not ready 4, 26 
adaptor not ready 4, 27 
adaptor not ready 4, 28 
adaptor not ready 4, 29 
error appending image 4 times 30 

什麼建議嗎?

非常感謝。

再見
托馬索

回答

1

writerInput.expectsMediaDataInRealTime = YES;

+0

謝謝,我加了這個命令(也檢查文件,它是在我的情況下,顯然需要),但結果是追加,然後再轉接器不會成爲準備 – Tommaso 2011-03-05 12:39:42

5

設置緩衝區時請嘗試此操作。

[adaptor appendPixelBuffer:buffer withPresentationTime:presentTime]; 
if(buffer) 
    CVBufferRelease(buffer); 
[NSThread sleepForTimeInterval:0.05]; 
+0

謝謝傑夫多了一個框架,它的作品吧!所有圖像都是在第一次嘗試後追加的 – Tommaso 2011-03-06 07:29:38

+1

爲什麼這個有效? – Praxiteles 2015-06-12 07:53:27

+0

爲我工作完美:) – 2016-05-18 13:49:19

1

在您的writerInput對象上使用requestMediaDataWhenReadyOnQueue:usingBlock:更合適。當它準備好再次輸入時,它會再次調用該塊。

+1

我仍然需要使用睡眠才能正常工作。可怕。 – 2012-01-05 16:43:23

-1

嘗試在循環內的代碼上使用@autoreleasepool。這將釋放每個循環內未使用的內存,而不是釋放所有內存直到循環結束。

+0

這個考慮更適合評論部分 – AlexeyVMP 2014-03-13 08:18:31