AVFoundation 5 Take out a still image from video

Swift 3.0 // // ViewController.swift // avfoundation005 // // Copyright © 2016年 FaBo, Inc. All rights reserved. // import UIKit import AVFoundation import AssetsLibrary class ViewController: UIViewController { override func viewDidLoad() { // パスからassetを生成. let path = Bundle.main.path(forResource: "test", ofType: "mov") let fileURL = URL(fileURLWithPath: path!) let avAsset = AVURLAsset(url: fileURL, options: nil) // assetから画像をキャプチャーする為のジュネレーターを生成. let generator = AVAssetImageGenerator(asset: avAsset) generator.maximumSize = self.view.frame.size // 動画の指定した時間での画像を得る(今回は動画の最後のキャプチャを撮る). let capturedImage = try! generator.copyCGImage(at: avAsset.duration, actualTime: nil) // 静止画用のImageViewを生成. let imageView = UIImageView(frame: self.view.frame) imageView.image = UIImage(cgImage: capturedImage) print(avAsset.duration) // imageViewをviewに追加. self.view.addSubview(imageView) } } Swift 2.3 // // ViewController.swift // avfoundation005 // // Copyright © 2016年 FaBo, Inc. All rights reserved. // import UIKit import AVFoundation import AssetsLibrary class ViewController: UIViewController { override func viewDidLoad() { // パスからassetを生成. let path = NSBundle.mainBundle().pathForResource("test", ofType: "mov") let fileURL = NSURL(fileURLWithPath: path!) let avAsset = AVURLAsset(URL: fileURL, options: nil) // assetから画像をキャプチャーする為のジュネレーターを生成. let generator = AVAssetImageGenerator(asset: avAsset) generator.maximumSize = self.view.frame.size // 動画の指定した時間での画像を得る(今回は動画の最後のキャプチャを撮る). let capturedImage = try! generator.copyCGImageAtTime(avAsset.duration, actualTime: nil) // 静止画用のImageViewを生成. let imageView = UIImageView(frame: self.view.frame) imageView.image = UIImage(CGImage: capturedImage) print(avAsset.duration) // imageViewをviewに追加. self.view.addSubview(imageView) } } 2.xと3.xの差分 NSBundle.mainBundle().pathForResource(String?, ofType: String?)からBundle.main.path(forResource: String?, ofType: String?)に変更 NSURLからURLに変更 AVURLAsset(URL: NSURL)にAVURLAsset(url: URL)に変更 AVAssetImageGenerator.copyCGImageAtTime(CMTime, actualTime: UnsafeMutablePointer<CMTime>)からAVAssetImageGenerator.copyCGImage(at: CMTime, actualTime: UnsafeMutablePointer<CMTime>?)に変更 UIImage(CGImage: CGImage)からUIImage(cgImage: CGImage)に変更

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.