如何快速实现AVAssetImageGenerator

如何快速实现AVAssetImageGenerator

到目前为止,我有以下内容

  let assetUrl = NSURL.URLWithString(self.targetVideoString)
  let asset: AVAsset = AVAsset.assetWithURL(assetUrl) as AVAsset
  let imageGenerator = AVAssetImageGenerator(asset: asset);
  let time : CMTime = CMTimeMakeWithSeconds(1.0, 1)
  let actualTime : CMTime
  let myImage: CGImage =imageGenerator.copyCGImageAtTime(requestedTime: time, actualTime:actualTime, error: <#NSErrorPointer#>)

最后一行是我迷路的地方...我只想在1.0秒的时间抓取图像

最佳答案

该函数声明为

func copyCGImageAtTime(requestedTime: CMTime, actualTime: UnsafeMutablePointer<CMTime>, error outError: NSErrorPointer) -> CGImage!

并且必须通过CMTime将(初始化的)NSError?&变量作为“输入输出表达式”传递:
let assetUrl = NSURL(string: ...)
let asset: AVAsset = AVAsset.assetWithURL(assetUrl) as AVAsset
let imageGenerator = AVAssetImageGenerator(asset: asset);
let time = CMTimeMakeWithSeconds(1.0, 1)

var actualTime : CMTime = CMTimeMake(0, 0)
var error : NSError?
let myImage = imageGenerator.copyCGImageAtTime(time, actualTime: &actualTime, error: &error)

另请注意,您的第一行
let assetUrl = NSURL.URLWithString(self.targetVideoString)

不再使用当前的Xcode 6.1进行编译。

关于swift - 如何快速实现AVAssetImageGenerator.copyCGImageAtTime,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26789211/

10-12 17:39