qq_34423626 2020-12-12 17:05 采纳率: 0%
浏览 87

iOS 大量图片合成视频内存暴涨问题

使用AVAssetWriter合成视频,内存暴涨,并且合成完不会释放内存,有大神看下改如何优化吗?下面是我的代码

AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:self.theVideoPath] fileType:AVFileTypeQuickTimeMovie error:&error];

    

    NSParameterAssert(videoWriter);

    if(error)

        NSLog(@"error =%@", [error localizedDescription]);

    //mov的格式设置 编码格式 宽度 高度

 

 

    NSDictionary *videoSettings =[NSDictionary dictionaryWithObjectsAndKeys:AVVideoCodecTypeH264,AVVideoCodecKey,

                                      [NSNumber numberWithInt:size.width],AVVideoWidthKey,

                                      [NSNumber numberWithInt:size.height],AVVideoHeightKey,nil];

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

 

    

    NSDictionary*sourcePixelBufferAttributesDictionary =[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:kCVPixelFormatType_32ARGB],kCVPixelBufferPixelFormatTypeKey,nil];

    //    AVAssetWriterInputPixelBufferAdaptor提供CVPixelBufferPool实例,

    //    可以使用分配像素缓冲区写入输出文件。使用提供的像素为缓冲池分配通常

    //    是更有效的比添加像素缓冲区分配使用一个单独的池

    AVAssetWriterInputPixelBufferAdaptor *adaptor =[AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput sourcePixelBufferAttributes:sourcePixelBufferAttributesDictionary];

    

    NSParameterAssert(writerInput);

    NSParameterAssert([videoWriter canAddInput:writerInput]);

    

    if ([videoWriter canAddInput:writerInput]){

        NSLog(@"11111");

    }

    else{

        NSLog(@"22222");

    }

    

    [videoWriter addInput:writerInput];

    

    [videoWriter startWriting];

    [videoWriter startSessionAtSourceTime:kCMTimeZero];

    

    //合成多张图片为一个视频文件

    dispatch_queue_t dispatchQueue =dispatch_queue_create("mediaInputQueue",NULL);

    int __block frame =0;

    [writerInput requestMediaDataWhenReadyOnQueue:dispatchQueue usingBlock:^{

        

        while([writerInput isReadyForMoreMediaData])

        {

            

            if(frame >=[self.libraryImages count])

            {

                [writerInput markAsFinished];

                [videoWriter finishWriting];

                

                NSString *exportPath = [[self createVideoFile] stringByAppendingPathComponent:[self movieName]];

                [self saveVideoWithVideoPath:self.theVideoPath andSavePath:exportPath];

                break;

            }

            CVPixelBufferRef buffer =NULL;

            int idx =frame;

            NSLog(@"idx==%d",idx);

            

            buffer = (CVPixelBufferRef)[self pixelBufferFromCGImage:[[self.libraryImages objectAtIndex:idx] CGImage] size:size];

            if (buffer)

            {

                if(![adaptor appendPixelBuffer:buffer withPresentationTime:CMTimeMake(frame,fps)])//设置每秒钟播放图片的个数

                {

                    NSLog(@"FAIL");

                    [writerInput markAsFinished];

                    [videoWriter finishWriting];

                    

                    NSString *exportPath = [[self createVideoFile] stringByAppendingPathComponent:[self movieName]];

                    [self saveVideoWithVideoPath:self.theVideoPath andSavePath:exportPath];

                    break;

                }

                else

                {

                    NSLog(@"OK");

                    frame++;

                }

                

                CFRelease(buffer);

            }

        }

    }];

  • 写回答

1条回答 默认 最新

  • Mr.Clark 2020-12-15 16:34
    关注

    用Profile里的Leaks运行看下;

    评论

报告相同问题?

悬赏问题

  • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?
  • ¥15 django5安装失败
  • ¥15 Java与Hbase相关问题
  • ¥15 后缀 crn 游戏文件提取资源
  • ¥20 bash代码推送不上去 git fetch origin master #失败了
  • ¥15 LOL外服加入了反作弊系统,现在游戏录像rofl文件离线都无法打开