21

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

谢谢!

4

6 回答 6

54

Objective-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

迅速

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 

斯威夫特 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)

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

于 2013-03-12T12:28:45.817 回答
3

试试这个

 - (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];

 }

希望这可以帮助 !!!

于 2013-03-12T12:15:41.807 回答
1

斯威夫特 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
}
于 2016-06-22T21:05:02.150 回答
0

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

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];    
于 2016-12-17T09:10:57.967 回答
0

这就是我如何在 Swift 场景中拍摄当前可见帧的方法:

关键是

  1. 获取类型为播放器的当前时间CMTime
  2. 将该时间转换为类型的秒数Float64
  3. 将 secondss 切换回CMTimeusing CMTimeMake。第一个参数应该是秒数的位置Int64

代码:

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)
}
于 2018-03-16T07:30:17.460 回答
0

用于从视频生成缩略图的 Swift 扩展

extension AVPlayer {
    func generateThumbnail(time: CMTime) -> UIImage? {
        guard let asset = currentItem?.asset else { return nil }
        let imageGenerator = AVAssetImageGenerator(asset: asset)

        do {
            let cgImage = try imageGenerator.copyCGImage(at: time, actualTime: nil)
            return UIImage(cgImage: cgImage)
        } catch {
            print(error.localizedDescription)
        }

        return nil
    }
}
于 2020-10-08T13:33:40.000 回答