我正在从视频中拍摄快照,它工作正常,但是我想要的是在拍摄快照之前,我想通过自定义宽度和高度,以便获得不同的图像尺寸而不是原始尺寸。
func loadVideoWithVideoURL(_ videoURL: URL) {
print("load video url \(videoURL)")
let asset = AVURLAsset(url: videoURL)
let generator = AVAssetImageGenerator(asset: asset)
generator.appliesPreferredTrackTransform = true
let time = NSValue(time: CMTimeMakeWithSeconds(CMTimeGetSeconds(asset.duration)/2, preferredTimescale: asset.duration.timescale))
generator.generateCGImagesAsynchronously(forTimes: [time]) { [weak self] _, image, _, _, _ in
if let image = image, let data = UIImage(cgImage: image).pngData() {
let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let uniqueImageName = videoURL.deletingPathExtension().lastPathComponent
print("/\(uniqueImageName).JPG")
let imageURL = urls[0].appendingPathComponent("\(uniqueImageName).jpg")
let videoURL1 = urls[0].appendingPathComponent("\(uniqueImageName).mov")
//writing data here
try? data.write(to: imageURL, options: [.atomic])
}
}
有什么建议吗?
预先感谢!