美文网首页
iOS开发笔记-103:Swift5 - 获取视频第一帧图片

iOS开发笔记-103:Swift5 - 获取视频第一帧图片

作者: 原味蛋炒饭 | 来源:发表于2020-02-18 10:57 被阅读0次
    avplay: AVPlayerItem
    let itemPath: AVURLAsset = avplay.asset as! AVURLAsset
    //获取视频第一帧图片
            let gen = AVAssetImageGenerator.init(asset: itemPath)
            gen.appliesPreferredTrackTransform = true
            let time = CMTimeMakeWithSeconds(0.0, preferredTimescale: 1)
            var actualTime : CMTime = CMTimeMakeWithSeconds(0, preferredTimescale: 0)
            do {
                let imageCg = try gen.copyCGImage(at: time, actualTime: &actualTime)
        //            let imgdata = image.jpegData(compressionQuality: scale);
                let image = UIImage.init(cgImage: imageCg)
                var data: Data!
                if image.pngData() == nil {
                    data = image.jpegData(compressionQuality: 1)
                } else {
                    data = image.pngData()
                }
                let how = data.count / 1024
                //        JJLog("how =\(how)kb\n")
                //比例
                let scale:CGFloat = (how > 200) ? (CGFloat(200/how)) : 1.0
                //压缩图片,图片上传不能大于200kb
                data = image.jpegData(compressionQuality: scale);
                let imageString = data.base64EncodedString()
    
                uploadVideoImgData(vImgDataStr: imageString)
            } catch  {
                print("错误")
            }
    

    相关文章

      网友评论

          本文标题:iOS开发笔记-103:Swift5 - 获取视频第一帧图片

          本文链接:https://www.haomeiwen.com/subject/omtgfhtx.html