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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With