iPhone中的AVAssetWriter的内存管理问题? [英] Memory Management issue with AVAssetWriter in iPhone?

查看:324
本文介绍了iPhone中的AVAssetWriter的内存管理问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have successfully created video from uiimages using AVAssetWriter. But as soon as the writer starts writing video theres a sudden rise in the memory allocation in the instruments. The spike in the memory allocation changes from 3-4 MB to 120MB and then cools off. I have used the following code for this...

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

请问有人可以帮助解决此问题,因为我过去两天都陷入了困境...

Can anybody plz help to resolve this problem as I am stuck with problem from last 2 days...

先感谢...

推荐答案

内存分配峰值仅在刺激器中出现,并且在设备上绝对正常. 我也已经成功完成了申请.我将其发布为仅供其他用户使用的答案,以便他们可以了解该工具工具中内存分配激增的原因.

The memory allocation peak was coming only in the stimulator and it works absolutely fine on the device. Also I have successfully completed the application. I am posting this as an answer just for other users so that they will know the reason of the spike in the memory allocation in the instruments tool.

这篇关于iPhone中的AVAssetWriter的内存管理问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆