Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a thumbnail or image of an AVPlayer at current time

Tags:

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

Thanks!

like image 700
kchromik Avatar asked Mar 12 '13 12:03

kchromik


Video Answer


1 Answers

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 

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

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.

like image 105
Dipen Panchasara Avatar answered Oct 08 '22 18:10

Dipen Panchasara