IOS 视频缩略图的生成

使用AVFoundation框架可以生成视频缩略图,用到的类:

》》AVAsset:

  用于获取多媒体的相关信息,如多媒体的画面和声音等。

》》AVURLAsset:  

  AVAsset的子类,用于根据NSURL生成AVAsset对象

》》AVAssetImageGenerator:

  用于截取视频指定帧的动画

一般步骤:

1、根据视频的URLchuang见AVURLAsset对象

2、根据AVURLAsset对象创建AVAssetImageGenerator对象

3、调用AVAssetImageGenerator对象的copyCGImageAtTime:actualTime:error:来获取该视频指定时间点的视频截图

  第一个参数:指定获取哪个时间点的视频截图,该参数是一个CMTime结构体:(CMTime. value/timescale = seconds)

typedef struct
{
    CMTimeValue    value;        /*! @field value The value of the CMTime. value/timescale = seconds. */
    CMTimeScale    timescale;    /*! @field timescale The timescale of the CMTime. value/timescale = seconds.  */
    CMTimeFlags    flags;        /*! @field flags The flags, eg. kCMTimeFlags_Valid, kCMTimeFlags_PositiveInfinity, etc. */
    CMTimeEpoch    epoch;        /*! @field epoch Differentiates between equal timestamps that are actually different because
                                                 of looping, multi-item sequencing, etc.  
                                                 Will be used during comparison: greater epochs happen after lesser ones. 
                                                 Additions/subtraction is only possible within a single epoch,
                                                 however, since epoch length may be unknown/variable. */
} CMTime;  

    获取CMTime相关函数:

CMTime CMTimeMake(
                int64_t value,        /*! @param value        Initializes the value field of the resulting CMTime. */
                int32_t timescale)    /*! @param timescale    Initializes the timescale field of the resulting CMTime. */
                            __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0);

CMTime CMTimeMakeWithSeconds(
                Float64 seconds,      
          int32_t preferredTimeScale)
                 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0);

  第二个参数:获取截图的实际时间点(要用“&”传指针)

  第三个参数:获取错误信息(要用“&”传指针)

posted @ 2016-07-28 14:48  熹微_念  阅读(803)  评论(0编辑  收藏  举报