zoukankan      html  css  js  c++  java
  • iOS截取视频某一帧图片(关键帧,AVAssetImageGenerator)

    获取第一帧图片

    导入 AVFoundation.Framework、CoreMedia.Framework

    实现代码例如以下:

    + (UIImage*) thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time {
        AVURLAsset *asset = [[[AVURLAsset alloc] initWithURL:videoURL options:nil] autorelease];
        NSParameterAssert(asset);
        AVAssetImageGenerator *assetImageGenerator = [[[AVAssetImageGenerator alloc] initWithAsset:asset] autorelease];
        assetImageGenerator.appliesPreferredTrackTransform = YES;
        assetImageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
    
        CGImageRef thumbnailImageRef = NULL;
        CFTimeInterval thumbnailImageTime = time;
        NSError *thumbnailImageGenerationError = nil;
        thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60) actualTime:NULL error:&thumbnailImageGenerationError];
    
        if (!thumbnailImageRef)
            NSLog(@"thumbnailImageGenerationError %@", thumbnailImageGenerationError);
    
        UIImage *thumbnailImage = thumbnailImageRef ?

    [[[UIImage alloc] initWithCGImage:thumbnailImageRef] autorelease] : nil; return thumbnailImage; }


    获取多帧图片的办法

    [mImageGenerator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:CMTimeMakeWithSeconds(time, NSEC_PER_SEC)]] completionHandler:
        ^(CMTime requestedTime, CGImageRef image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error)
        {
     
            NSLog(@"actual got image at time:%f", CMTimeGetSeconds(actualTime));
            if (image)
            {
                [CATransaction begin];
                [CATransaction setDisableActions:YES];
                [layer setContents:(id)image];
     
                //UIImage *img = [UIImage imageWithCGImage:image];
                //UIImageWriteToSavedPhotosAlbum(img, self, nil, nil);
     
                [CATransaction commit];
            }
        }];
     


  • 相关阅读:
    LeetCode 75. Sort Colors(按颜色进行排序)
    LeetCode 451. Sort Characters By Frequency(按照字符出现次数对字符串排序)
    LeetCode 347. Top K Frequent Elements(出现频率最多的 k 个元素)
    LeetCode 215. Kth Largest Element in an Array(数组求第k大)
    CF #629 Div.3 E(LCA)F
    系统函数
    CASE表达式
    循环得出数据库中所有的 DB_ID,DB_NAME
    数据库的编码问题
    检验临时表是否存在
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/6911856.html
Copyright © 2011-2022 走看看