4

我已经使用 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];
}

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

提前谢谢...

4

2 回答 2

3

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

于 2010-11-01T15:02:11.487 回答
-1

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

于 2012-02-21T14:34:02.320 回答