【问题标题】:Memory Management issue with AVAssetWriter in iPhone?iPhone 中 AVAssetWriter 的内存管理问题?
【发布时间】:2011-05-03 10:38:02
【问题描述】:

我已经使用 AVAssetWriter 从 uiimages 成功创建了视频。但是一旦作者开始写视频,仪器中的内存分配就会突然增加。内存分配的峰值从 3-4 MB 变为 120MB,然后冷却下来。我为此使用了以下代码...

-(void)writeImageAsMovie:(NSArray *)array toPath:(NSString*)path size:(CGSize)size
{
NSMutableDictionary *attributes = [[NSMutableDictionary alloc]init];
[attributes setObject:[NSNumber numberWithUnsignedInt:kCVPixelFormatType_32ARGB] forKey:(NSString*)kCVPixelBufferPixelFormatTypeKey];
[attributes setObject:[NSNumber numberWithUnsignedInt:320] forKey:(NSString*)kCVPixelBufferWidthKey];
[attributes setObject:[NSNumber numberWithUnsignedInt:416] forKey:(NSString*)kCVPixelBufferHeightKey];

NSError *error = nil;
AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
                              [NSURL fileURLWithPath:path] fileType:AVFileTypeQuickTimeMovie
                                                          error:&error];
NSParameterAssert(videoWriter);

NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                               AVVideoCodecH264, AVVideoCodecKey,
                               [NSNumber numberWithInt:size.width], AVVideoWidthKey,
                               [NSNumber numberWithInt:size.height], AVVideoHeightKey,
                               nil];

AVAssetWriterInput* writerInput = [[AVAssetWriterInput
                                    assetWriterInputWithMediaType:AVMediaTypeVideo
                                    outputSettings:videoSettings] retain];

adaptor = [AVAssetWriterInputPixelBufferAdaptor
                                                 assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput
                                                 sourcePixelBufferAttributes:attributes];

NSParameterAssert(writerInput);
NSParameterAssert([videoWriter canAddInput:writerInput]);
[videoWriter addInput:writerInput];


//Start a session:
[videoWriter startWriting];
[videoWriter startSessionAtSourceTime:kCMTimeZero];

CVPixelBufferRef buffer = NULL;
buffer = [self pixelBufferFromCGImage:[[array objectAtIndex:0] CGImage]];
[adaptor appendPixelBuffer:buffer withPresentationTime:kCMTimeZero];

//Write samples:
for (int i = 0;i<[array count]; i++)
{
    if([writerInput isReadyForMoreMediaData])
    {
        NSLog(@"inside for loop %d",i);
        CMTime frameTime = CMTimeMake(1, 20);

        CMTime lastTime=CMTimeMake(i, 20); //i is from 0 to 19 of the loop above

        CMTime presentTime=CMTimeAdd(lastTime, frameTime);

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

        [adaptor appendPixelBuffer:buffer withPresentationTime:presentTime];

        if(buffer)
            CVBufferRelease(buffer);
    }
    else
    {
        NSLog(@"error");
        i--;
    }

}

//Finish the session:
[writerInput markAsFinished];
[videoWriter finishWriting];

NSURL *pathURL = [NSURL fileURLWithPath:path];

AVURLAsset *url = [[AVURLAsset alloc] initWithURL:pathURL options:nil];

[clipsArray addObject:url];
[url release];
CVPixelBufferPoolRelease(adaptor.pixelBufferPool);
[videoWriter release];
[writerInput release];
[imageArray removeAllObjects];
}

任何人都可以帮助解决这个问题,因为我在过去 2 天遇到问题......

提前谢谢...

【问题讨论】:

  • 这个问题只出现在刺激器上,不会出现在设备上。
  • 我可以知道这段代码中数组的使用吗?

标签: iphone objective-c memory-management avfoundation avassetwriter


【解决方案1】:

内存分配峰值仅出现在刺激器中,它在设备上运行良好。 我也成功完成了申请。我将其发布为仅针对其他用户的答案,以便他们知道仪器工具中内存分配峰值的原因。

【讨论】:

  • 这不是原因,只是运气好。
【解决方案2】:

我认为问题在于您在循环中运行它,而不是给 RunLoop 任何更改来执行自动释放实例的垃圾收集。

【讨论】:

  • 你能给我指出正确的方向来实现这个吗...你能告诉我一些代码如何在上面的代码中使用 RunLoop,因为我从未使用过 RunLoop。
  • 我不太了解 AVFoundation。是不是有一些示例代码可以查看?
  • 感谢您的回复...内存分配峰值仅出现在刺激器中,并且它在设备上工作得非常好。
猜你喜欢
  • 2011-01-07
  • 2011-04-11
  • 1970-01-01
  • 1970-01-01
  • 2011-06-06
  • 2010-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多