Why AVAssetImageGenerator generateCGImagesAsynchronouslyForTimes crashes the app

[亡魂溺海] 提交于 2019-12-11 05:36:42

问题


I am trying to extract 2 frames per second from a video using generateCGImagesAsynchronouslyForTimes. But my app crashes. I am monitoring the memory usage but its not going any way up than 14 mb.

Here is the code:

- (void) createImagesFromVideoURL:(NSURL *) videoUrl atFPS: (int) reqiuredFPS completionBlock: (void(^) (NSMutableArray *frames, CGSize frameSize)) block
{
    NSMutableArray *requiredFrames = [[NSMutableArray alloc] init];

    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoUrl options:nil];

    AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];

    generator.requestedTimeToleranceAfter =  kCMTimeZero;

    generator.requestedTimeToleranceBefore =  kCMTimeZero;

    generator.appliesPreferredTrackTransform = YES;

    UIImage *sampleGeneratedImage;

    for (Float64 i = 0; i < CMTimeGetSeconds(asset.duration) *  reqiuredFPS ; i++)
    {
        CMTime time = CMTimeMake(i, reqiuredFPS);

        NSError *err;

        CMTime actualTime;

        CGImageRef image = [generator copyCGImageAtTime:time actualTime:&actualTime error:&err];

        if (! err)
        {
            sampleGeneratedImage = [[UIImage alloc] initWithCGImage:image];
            break;
        }
    }

    //Get the maximum size from 1 frame
    generator.maximumSize = [self getMultipleOf16AspectRatioForCurrentFrameSize:sampleGeneratedImage.size];

    NSMutableArray *requestedFrameTimes = [[NSMutableArray alloc] init];

    for (Float64 i = 0; i < CMTimeGetSeconds(asset.duration) *  reqiuredFPS ; i++)
    {
        CMTime time = CMTimeMake(i, reqiuredFPS);

        [requestedFrameTimes addObject:[NSValue valueWithCMTime:time]];
    }

    [generator generateCGImagesAsynchronouslyForTimes:[requestedFrameTimes copy] completionHandler:^(CMTime requestedTime, CGImageRef  _Nullable image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError * _Nullable error) {

        if (image)
        {
            UIImage *generatedImage = [UIImage imageWithCGImage:image];

            [requiredFrames addObject:generatedImage];
        }

        if (CMTimeCompare(requestedTime, [[requestedFrameTimes lastObject] CMTimeValue]) == 0)
        {
            NSLog(@"Image processing complete");

            dispatch_async(dispatch_get_main_queue(), ^{
                block(requiredFrames, generator.maximumSize);
            });
        }

        else
        {
            NSLog(@"Getting frame at %lld", actualTime.value/actualTime.timescale);
        }
    }];
}

来源:https://stackoverflow.com/questions/44910012/why-avassetimagegenerator-generatecgimagesasynchronouslyfortimes-crashes-the-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!