iOS从图像创建视频并获得EXC_BAD_ACCESS进行第二次尝试

时间:2017-10-18 21:18:18

标签: ios objective-c image video

我制作应用程序是什么使图像中的视频。 在这里,我创建了一个新数组:

    self.imageList = [NSMutableArray<UIImage *> new];

然后向此阵列添加大量图像。

NSArray   *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString  *documentsDirectory = [paths objectAtIndex:0];


    for (NSString *framePath in _effect.framePathList)
    {
        @autoreleasepool
        {
            NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,framePath];
            UIImage *frame = [[UIImage alloc] initWithContentsOfFile:filePath];

            i++;
            self.selectedEffect.image = [self applyEffect:frame];
                CGRect rect = [_backgroundView bounds];
                UIGraphicsBeginImageContextWithOptions(rect.size,NO,0.0f);
                CGContextRef context = UIGraphicsGetCurrentContext();
                [self.backgroundView.layer renderInContext:context];
                UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();

                UIGraphicsEndImageContext();

                if (capturedImage.scale * capturedImage.size.height > 640)
                {
                    CGSize newSize = CGSizeMake(640, 640);
                    UIGraphicsBeginImageContext(newSize);
                    [capturedImage drawInRect:CGRectMake(0, 0, newSize.height, newSize.height)];
                    UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();
                    capturedImage = destImage;
                    destImage = nil;
                    UIGraphicsEndImageContext();
                }

                dispatch_sync(dispatch_get_main_queue(), ^
                              {
                                  if (self.updateLoadingState)
                                  {
                                      self.updateLoadingState((float)i / framaCount * 100.0);
                                  }
                              });
                if (capturedImage)
                {
                    [_imageList addObject:capturedImage];
                }

        }
    }

然后尝试制作视频。这是方法

[self.movieMaker createMovieFromImages:_imageList withCompletion:^(NSURL *fileURL)
     {
         {
             if (_finishBlock)
             {
                 _finishBlock();
             }
         }
     }];

此方法位于movieMaker类中。

- (void) createMovieFromSource:(NSArray *)images extractor:(CEMovieMakerUIImageExtractor)extractor withCompletion:(CEMovieMakerCompletion)completion {self.completionBlock = completion;

    [self.assetWriter startWriting];
    [self.assetWriter startSessionAtSourceTime:kCMTimeZero];

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

    __block NSInteger i = 0;

    NSInteger frameNumber = [images count];

    [self.writerInput requestMediaDataWhenReadyOnQueue:mediaInputQueue usingBlock:^{
        while (YES)
        {
            @autoreleasepool
            {
                if (i >= frameNumber)
                {
                    break;
                }
                if ([self.writerInput isReadyForMoreMediaData])
                {
                    CVPixelBufferRef sampleBuffer;

                    NSMutableArray *test = [NSMutableArray arrayWithArray:images];
                    UIImage *img = extractor([test objectAtIndex:i]);
                    if (img == nil) {
                        i++;
                        NSLog(@"Warning: could not extract one of the frames");
                        continue;
                    }

                    CGImageRef img2 = [img CGImage];
                    sampleBuffer = [self newPixelBufferFromCGImage:img2];
                    CGImageRelease(img2);

                    if (sampleBuffer) {
                        if (i == 0)
                        {
                            [self.bufferAdapter appendPixelBuffer:sampleBuffer withPresentationTime:kCMTimeZero];
                        }
                        else
                        {
                            CMTime lastTime = CMTimeMake(i-1, self.frameTime.timescale);
                            CMTime presentTime = CMTimeAdd(lastTime, self.frameTime);
                            [self.bufferAdapter appendPixelBuffer:sampleBuffer withPresentationTime:presentTime];
                        }
                        CFRelease(sampleBuffer);

                        i++;
                    }
                }
            }
        }
        [self.writerInput markAsFinished];
        [self.assetWriter finishWritingWithCompletionHandler:^{
            dispatch_async(dispatch_get_main_queue(), ^{
                self.completionBlock(self.fileURL);
            });
        }];

        CVPixelBufferPoolRelease(self.bufferAdapter.pixelBufferPool);
    }];

}

没有CGImageRelease(img2);一切正常但我有大量的内存泄漏。我在阵列中有超过100个图像。如果我使用CGImageRelease(img2);第一次运行就没问题,没有内存泄漏。但是,如果我再次尝试,我会在此行中获得EXC_BAD_ACCESS:self.imageList = [NSMutableArray<UIImage *> new]; 这是error 我能做什么?提前谢谢!

0 个答案:

没有答案