保存图像序列时的内存警告

时间:2013-10-22 13:25:36

标签: cocoa memory video warnings frame

我正在尝试将视频转换为一系列图像帧并将其保存到文件中。然而,在大约5帧后,我收到内存警告,应用程序因内存压力很快就会退出。图像大小约为10kb。我的代码在下面,我做错了什么?

提前致谢,Ben。

-(void)framesFromVideo:(AVURLAsset *)asset{
    NSArray *times = [self timeSequenceFromAsset:asset];

    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *folderPath = rootPath;

    __block int i=0;
    generator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
    generator.apertureMode = AVAssetImageGeneratorApertureModeCleanAperture;
    generator.appliesPreferredTrackTransform = TRUE;
    generator.requestedTimeToleranceBefore = kCMTimeZero;
    generator.requestedTimeToleranceAfter = kCMTimeZero;
    [generator generateCGImagesAsynchronouslyForTimes:times completionHandler:^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){

        if (result == AVAssetImageGeneratorSucceeded) {
            NSString *imgPath = [folderPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png", i]];
            [self writeImageRef:im toPath:imgPath];
        }else if (result == AVAssetImageGeneratorFailed){
            NSLog(@"Frame generation failed with error:%@", error);
        }else if (result == AVAssetReaderStatusCancelled){
            NSLog(@"Frame generation cancelled");
        }
        NSLog(@"Completed frame");
        i++;
    }];
}

1 个答案:

答案 0 :(得分:0)

我在类似的实现中遇到了这个问题,但我没有使用完成块。 为了解决我的“记忆压力”,我不得不释放CGImageRef。

尝试添加...

CFRelease(im);

所以......

-(void)framesFromVideo:(AVURLAsset *)asset{
    NSArray *times = [self timeSequenceFromAsset:asset];

    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *folderPath = rootPath;

    __block int i=0;
    generator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
    generator.apertureMode = AVAssetImageGeneratorApertureModeCleanAperture;
    generator.appliesPreferredTrackTransform = TRUE;
    generator.requestedTimeToleranceBefore = kCMTimeZero;
    generator.requestedTimeToleranceAfter = kCMTimeZero;
    [generator generateCGImagesAsynchronouslyForTimes:times completionHandler:^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){

        if (result == AVAssetImageGeneratorSucceeded) {
            NSString *imgPath = [folderPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png", i]];
            [self writeImageRef:im toPath:imgPath];
            CFRelease(im);
        }else if (result == AVAssetImageGeneratorFailed){
            NSLog(@"Frame generation failed with error:%@", error);
        }else if (result == AVAssetReaderStatusCancelled){
            NSLog(@"Frame generation cancelled");
        }
        NSLog(@"Completed frame");
        i++;
    }];
}