xcode 在当前时间创建 AVPlayer 的缩略图或图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15360721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:57:18  来源:igfitidea点击:

Create a thumbnail or image of an AVPlayer at current time

objective-cxcodeipad

提问by kchromik

I have implemented an AVPlayerand i want to take an image or thumbnail when clicking on a toolbar button and open in a new UIViewControllerwith UIImageView. The image should be scaled exactly like the AVPlayer. The segueis already working, i just have to implement that i get the image at the current play time.

我已经实现了AVPlayer,我想在单击工具栏按钮时拍摄图像或缩略图,并UIViewControllerUIImageView. 图像应该像AVPlayer. 在segue已经工作,我只是要实现,我在目前的播放时间获取图像。

Thanks!

谢谢!

回答by Dipen Panchasara

Objective-C

目标-C

AVAsset *asset = [AVAsset assetWithURL:sourceURL];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
CMTime time = CMTimeMake(1, 1);
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);  // CGImageRef won't be released by ARC

Swift

迅速

var asset = AVAsset.assetWithURL(sourceURL)
var imageGenerator = AVAssetImageGenerator(asset: asset!)
var time = CMTimeMake(1, 1)
var imageRef = try! imageGenerator!.copyCGImageAtTime(time, actualTime: nil)
var thumbnail = UIImage.imageWithCGImage(imageRef)
CGImageRelease(imageRef) // CGImageRef won't be released by ARC 

Swift 3.0

斯威夫特 3.0

var sourceURL = URL(string: "Your Asset URL")
var asset = AVAsset(url: sourceURL!)
var imageGenerator = AVAssetImageGenerator(asset: asset)
var time = CMTimeMake(1, 1)
var imageRef = try! imageGenerator.copyCGImage(at: time, actualTime: nil)
var thumbnail = UIImage(cgImage:imageRef)

Note :Interpret Swift code according to your swift version.

注意:根据您的 swift 版本解释 Swift 代码。

回答by arun.s

Try this

尝试这个

 - (UIImage*)takeScreeenShot {

 AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:vidURL
 options:nil];

 AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];

 imageGenerator.appliesPreferredTrackTransform = YES;

 NSError *err = NULL;

 CMTime time = CMTimeMake(1, 60); // time range in which you want
 screenshot

 CGImageRef imgRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL
 error:&err];

 return [[UIImage alloc] initWithCGImage:imgRef];

 }

Hope this helps !!!

希望这可以帮助 !!!

回答by JAL

Swift 2.x:

斯威夫特 2.x:

let asset = AVAsset(...)
let imageGenerator = AVAssetImageGenerator(asset: asset)
let screenshotTime = CMTime(seconds: 1, preferredTimescale: 1)
if let imageRef = try? imageGenerator.copyCGImageAtTime(screenshotTime, actualTime: nil) {

    let image = UIImage(CGImage: imageRef)

    // do something with your image
}

回答by G. Hazarath Reddy

Add below code to generate thumbnail from video.

添加以下代码以从视频生成缩略图。

AVURLAsset *assetURL = [[AVURLAsset alloc] initWithURL:partOneUrl options:nil]; 

AVAssetImageGenerator *assetGenerator = [[AVAssetImageGenerator alloc] initWithAsset:assetURL]; 

assetGenerator.appliesPreferredTrackTransform = YES;  

NSError *err = NULL; 

CMTime time = CMTimeMake(1, 2);     

CGImageRef imgRef = [assetGenerator copyCGImageAtTime:time actualTime:NULL error:&err]; 

UIImage *one = [[UIImage alloc] initWithCGImage:imgRef];    

回答by Lance Samaria

This is how I get a shot of the current visible frame on the scene in Swift:

这就是我如何在 Swift 中获取场景中当前可见帧的快照:

The key is to

关键是

  1. get the current time of the player which is of type CMTime
  2. convert that time into seconds of type Float64
  3. switch the secondss back to CMTimeusing CMTimeMake. The first parameter which would be where the seconds goes should be cast to Int64
  1. 获取类型为玩家的当前时间 CMTime
  2. 将该时间转换为秒类型 Float64
  3. 将秒切换回CMTime使用CMTimeMake. 第一个参数是秒的位置应该被转换为Int64

Code:

代码:

var myImage: UIImage?

guard let player = player else { return }

let currentTime: CMTime = player.currentTime() // step 1.
let currentTimeInSecs: Float64 = CMTimeGetSeconds(currentTime) // step 2.
let actionTime: CMTime = CMTimeMake(Int64(currentTimeInSecs), 1) // step 3.

let asset = AVAsset(url: fileUrl)
let imageGenerator = AVAssetImageGenerator(asset: asset)
imageGenerator.appliesPreferredTrackTransform = true // prevent image rotation

do{
    let imageRef =  try imageGenerator.copyCGImage(at: actionTime, actualTime: nil)

     myImage = UIImage(cgImage: imageRef)
}catch let err as NSError{
    print(err.localizedDescription)
}