another.im-ios/AnotherIM/Helpers/AVAsset+Thumbnail.swift

17 lines
623 B
Swift
Raw Permalink Normal View History

import AVFoundation
import UIKit
extension AVAsset {
func generateVideoThumbnail(_ size: CGSize) async throws -> UIImage {
try await Task {
let assetImgGenerate = AVAssetImageGenerator(asset: self)
assetImgGenerate.appliesPreferredTrackTransform = true
let time = CMTimeMakeWithSeconds(Float64(1), preferredTimescale: 600)
let cgImage = try assetImgGenerate.copyCGImage(at: time, actualTime: nil)
let image = UIImage(cgImage: cgImage)
let result = try await image.scaleAndCropImage(size)
return result
}.value
}
}