截取AVPlayer的截图但它适用于某些视频

时间:2016-05-14 09:37:36

标签: ios swift screenshot avplayer

我正在拍摄avPlayer的截图..

    print(startTime)
    print(Float64(startTime))
    var time: CMTime = CMTimeMakeWithSeconds(Float64(startTime), 100000000)

    do {
        print(time)
    let imageRef: CGImageRef = try imageGenerator.copyCGImageAtTime(time, actualTime: &time)
    let thumbnail: UIImage = UIImage(CGImage: imageRef)



   UIImageWriteToSavedPhotosAlbum(thumbnail, nil, nil, nil)

在这里,我正在获取截图,但是当我想要它时它并不完美..但是对于一些视频来说它工作得很完美..请指导我这个感谢..

1 个答案:

答案 0 :(得分:2)

得到了确切的解决方案......

func screenshotCMTime(cmTime: CMTime)  -> (UIImage)?
{
guard let player = player,let asset = player.currentItem?.asset else 
{
            return nil
 }
        let imageGenerator = AVAssetImageGenerator(asset: asset)

        var timePicture = kCMTimeZero
        imageGenerator.appliesPreferredTrackTransform = true
        imageGenerator.requestedTimeToleranceAfter = kCMTimeZero
        imageGenerator.requestedTimeToleranceBefore = kCMTimeZero
        do {
        let ref = try imageGenerator.copyCGImageAtTime(cmTime, actualTime: &timePicture)

        image = UIImage(CGImage: ref)
        }catch {
            error as NSError

        }
        return image
    }