【问题标题】:iPhone - Received memory warningiPhone - 收到内存警告
【发布时间】:2012-07-25 06:27:31
【问题描述】:

我正在使用 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 循环执行时,它会打印我输入的日志,并增加整数 val。 在这之间,有时 GDB 中会显示一条消息“Received Memory Warning”。

非常感谢...

【问题讨论】:

  • 你对那个内存警告有什么反应?你的问题到底是什么?您是否正在调查潜在的内存泄漏?如果是这样,您是否尝试过分析工具?
  • 您没有显示足够的代码。例如,[self imageFromSampleBuffer:] 是什么?我们不知道,那么我们怎么知道您可能在该方法中使用内存做什么呢?没有理由认为问题出在您恰好在此处显示的 sn-p 中。

标签: iphone objective-c memory-management avfoundation


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    移动你的

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

    到 while() 循环内的行,并放一个

      [pool drain];
    

    就在while循环结束之前。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-28
      • 1970-01-01
      相关资源
      最近更新 更多