2

我正在使用 aVFoundation 框架从视频资产中读取 CMSampleBufferRef,在一段时间循环中,我得到如下图像:

-(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]);
}

当 while 循环正在执行时,它会打印我输入的日志,并增加 integer val。在这之间有时会在 GDB 中显示一条消息“收到内存警告”。

非常感谢...

4

2 回答 2

2

听起来您需要考虑将 while 循环的每次迭代都包装在自动释放池中https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html

于 2012-09-07T21:04:27.163 回答
0

移动你的

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

行到 while() 循环内,然后放一个

  [pool drain];

就在while循环结束之前。

此外,根据您拥有的图像数量,您将耗尽内存,因为您将缩小的图像保留在内存中......

于 2012-12-20T20:42:12.420 回答