Creating thumbnail from local video in swift

前端 未结 7 2181
挽巷
挽巷 2020-12-04 06:59

How to create thumbnail in swift from a local video file ?

For example if the video file path is located here :

file:///Users/Dev/Library/Developer/Co

相关标签:
7条回答
  • 2020-12-04 07:46

    BaseZen's answer translated Swift 3 / Swift 4

    You need to set the location of the video you want to make a thumbnail of as the url asset path, like:

    Don't forget to import AVFoundation

    func generateThumbnail(path: URL) -> UIImage? {
        do {
            let asset = AVURLAsset(url: path, options: nil)
            let imgGenerator = AVAssetImageGenerator(asset: asset)
            imgGenerator.appliesPreferredTrackTransform = true
            let cgImage = try imgGenerator.copyCGImage(at: CMTimeMake(value: 0, timescale: 1), actualTime: nil)
            let thumbnail = UIImage(cgImage: cgImage)
            return thumbnail
        } catch let error {
            print("*** Error generating thumbnail: \(error.localizedDescription)")
            return nil
        }
    }
    

    For everyone having issues with this I have created the following drop in example hosted on Github

    0 讨论(0)
提交回复
热议问题