Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iPhone中AVAssetWriter的内存管理问题?_Iphone_Objective C_Memory Management_Avfoundation_Avassetwriter - Fatal编程技术网

iPhone中AVAssetWriter的内存管理问题?

iPhone中AVAssetWriter的内存管理问题?,iphone,objective-c,memory-management,avfoundation,avassetwriter,Iphone,Objective C,Memory Management,Avfoundation,Avassetwriter,我已经使用AVAssetWriter从uiimages成功创建了视频。但一旦作者开始写视频,仪器中的内存分配就会突然增加。内存分配的峰值从3-4MB变为120MB,然后冷却。我已经为此使用了以下代码 -(void)writeImageAsMovie:(NSArray *)array toPath:(NSString*)path size:(CGSize)size { NSMutableDictionary *attributes = [[NSMutableDictionary alloc]ini

我已经使用AVAssetWriter从uiimages成功创建了视频。但一旦作者开始写视频,仪器中的内存分配就会突然增加。内存分配的峰值从3-4MB变为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];
}
-(void)writeImageAsMovie:(NSArray*)数组toPath:(NSString*)路径大小:(CGSize)大小
{
NSMutableDictionary*属性=[[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];
n错误*错误=nil;
AVAssetWriter*videoWriter=[[AVAssetWriter alloc]initWithURL:
[NSURL fileURLWithPath:path]文件类型:AVFileTypeQuickTimeMovie
错误:&错误];
NSParameterAssert(视频编写器);
NSDictionary*videoSettings=[NSDictionary Dictionary WithObjectsAndKeys:
AVVideoCodecH264,AVVideoCodeKey,
[NSNumber numberWithInt:size.width],AVVideoWidthKey,
[NSNumber numberWithInt:size.height],AVVideoHeightKey,
零];
AVAssetWriterInput*writerInput=[[AVAssetWriterInput]
assetWriterInputWithMediaType:AVMediaTypeVideo
输出设置:视频设置]保留];
适配器=[AvassetWriterInputPixelBufferAdapter]
AssetWriterInputPixelBufferAdapter与AssetWriterInput:writerInput
sourcePixelBufferAttributes:属性];
NSParameterAssert(writerInput);
NSParameterAssert([videoWriter canAddInput:writerInput]);
[videoWriter附加输入:writerInput];
//启动会话:
[videoWriter startWriting];
[videoWriter startSessionAtSourceTime:kCMTimeZero];
CVPixelBufferRef buffer=NULL;
缓冲区=[self pixelBufferFromCGImage:[[array objectAtIndex:0]CGImage]];
[PixelBuffer:BufferwithPresentationTime:kCMTimeZero];
//编写示例:

对于(int i=0;i我认为问题在于,您是在循环中运行它,没有给RunLoop任何更改来对自动释放的实例进行垃圾收集。

内存分配峰值只出现在刺激器中,它在设备上完全正常工作。
另外,我已经成功地完成了应用程序。我将此作为一个答案发布给其他用户,以便他们知道instruments tool中内存分配尖峰的原因。

你能为我指出正确的方向来实现这一点吗…你能给我展示一些我从未使用过的代码如何在上面的代码中使用RunLoop吗d RunLoop。我不太了解AVFoundation。是否有一些示例代码可供您查看?Thanx供您回复…内存分配峰值仅出现在刺激器中,并且在设备上运行完全正常。此问题仅出现在刺激器中,而不出现在设备上。我可以在中使用数组吗这个密码?这不是原因,只是幸运而已。