2012-07-25 121 views
2

我從視頻資源的使用aVFoundation框架閱讀CMSampleBufferRef,在一個while循環,我收到那些圖像象下面這樣:當while循環執行它iPhone - 收到內存警告

-(void)splitImagesFromVideo 
{ 
    if (images != nil) { 
     [images release]; 
     images = nil; 
    } 
    images =[[NSMutableArray alloc] init]; 


    NSURL *fileURL = [[NSBundle mainBundle] 
         URLForResource:@"3idiots" withExtension:@"mov"]; 

    NSLog(@"video: %@",videoURL); 

    AVAsset *theAVAsset = [[AVURLAsset alloc] initWithURL:videoURL options:nil]; 

    NSError *error = nil; 
    float width = theAVAsset.naturalSize.width; 
    float height = theAVAsset.naturalSize.height; 
    AVAssetReader *mAssetReader = [[AVAssetReader alloc] initWithAsset:theAVAsset error:&error]; 



    NSArray *videoTracks = [theAVAsset tracksWithMediaType:AVMediaTypeVideo]; 
    AVAssetTrack *videoTrack = [videoTracks objectAtIndex:0]; 


    NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]; 
    AVAssetReaderTrackOutput* mAssetReaderOutput = [[AVAssetReaderTrackOutput alloc] initWithTrack:videoTrack outputSettings:options]; 

    [mAssetReader addOutput:mAssetReaderOutput]; 



    BOOL success = [mAssetReader startReading]; 
    NSInteger val = 1; 

    while ([mAssetReader status]==AVAssetReaderStatusReading){ 
     CMSampleBufferRef buffer = [mAssetReaderOutput copyNextSampleBuffer];//read next image. 
     if (buffer) { 

      UIImage *img = [self imageFromSampleBuffer:buffer]; 
      if (img != nil) { 

       NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
       img = [self imageOfSize:CGSizeMake(320, 480) fromImage:img]; 
       [images addObject:img]; 
       [pool release]; 
      } 

      CMSampleBufferInvalidate(buffer); 
      CFRelease(buffer); 
      buffer = nil; 

     } 
     NSLog(@"value: %d", val++); 
    } 

    [images removeLastObject]; 
    NSLog(@"img count: %d", [images count]); 
    NSLog(@"imgs: %@",images); 

    [theAVAsset release]; 
    [mAssetReader release]; 
    [mAssetReaderOutput release]; 
    NSLog(@"count: %d", [images count]); 
} 

打印我把日誌遞增整數val。 ,並且在這段時間之間在GDB中顯示消息「接收到的內存警告」。

非常感謝......

+0

你怎麼到內存警告作出反應?你的問題到底是什麼?你在調查潛在的內存泄漏嗎?如果是這樣,你是否嘗試過分析儀工具? – 2012-07-25 07:09:50

+0

您沒有顯示足夠的代碼。例如,什麼是'[self imageFromSampleBuffer:]'?我們不知道,那麼我們怎麼知道你在記憶中可能用這種方法做什麼?沒有理由認爲問題出現在你剛纔在這裏展示的片段中。 – matt 2012-09-07 21:11:58

回答

0

只是內將您的

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

線while()循環,並且放一個

[pool drain]; 

就在此之前while循環的結尾。

此外,這取決於你有多少圖像有,你會耗盡內存,因爲你保持你的內存縮小圖像...