无法查看视频的每个帧

时间:2017-08-08 18:17:05

标签: ios objective-c avfoundation avasset

我试图逐帧阅读视频,并使用here提供的答案来做同样的事情。我的问题是我无法查看创建的图像。

我尝试将其写入PNG文件(注释代码),这似乎正在起作用。有没有理由说UIImageView没有显示UIImage?我尝试使用dispatch_async在主线程上运行它。那也没有解决问题。我在iOS 10.3.3上

这就是我正在做的事情:

    NSURL *url = [[NSBundle mainBundle] URLForResource:@"recording2" withExtension:@"mov"];
    AVURLAsset* asset = [AVURLAsset URLAssetWithURL:url options:nil];

    NSArray *movieTracks = [asset tracksWithMediaType:AVMediaTypeVideo];
    AVAssetTrack *movieTrack = [movieTracks objectAtIndex:0];

    AVAssetImageGenerator* generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
        generator.appliesPreferredTrackTransform = YES;

    //Create a variables for the time estimation
    Float64 durationSeconds = CMTimeGetSeconds(asset.duration);
    Float64 timePerFrame = 1.0 / (Float64)movieTrack.nominalFrameRate;
    Float64 totalFrames = durationSeconds * movieTrack.nominalFrameRate;

    //Step through the frames
    for (int counter = 0; counter <= totalFrames; counter++){

//        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//        
//        NSString *folderName = [NSString stringWithFormat:@"/%d.png", counter];
//        NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:folderName];
//        
//        // Save image.
//        [UIImagePNGRepresentation(image2) writeToFile:filePath atomically:YES];

        CMTime actualTime;
        Float64 secondsIn = ((float)counter/totalFrames)*durationSeconds;
        CMTime imageTimeEstimate = CMTimeMakeWithSeconds(secondsIn, 600);
        NSError *error;
        CGImageRef image = [generator copyCGImageAtTime:imageTimeEstimate actualTime:&actualTime error:&error];
        NSLog(@"Error %@", error.description); // Always (null)
        imageView.image = [UIImage imageWithCGImage:image];

        CGImageRelease(image);

感谢。

0 个答案:

没有答案