これは私にとってはうまくいきます。スライド変更時にビデオ アセットからフレームを取得します。以下のコードを参照してください。これは私の現在のプロジェクトから切り取られたもので、いくつかのエラーが含まれている可能性がありますが、アイデアは得られます。これは Swift 1 にありますが、問題ないことを願っています。
var urlVideo :NSURL = NSURL()
var vidLength :CMTime = CMTime()
var durInSeconds: Float = 0.00
var timescale:CMTimeScale = CMTimeScale()
var videoInfo:NSDictionary = NSDictionary()
var chosePosSlider:UISlider = UISlider()
@IBOutlet weak var imgCont: UIImageView!
func generateThumnail(url : NSURL, fromTime:Float64) -> UIImage {
var asset :AVAsset = AVAsset.assetWithURL(url) as! AVAsset
var assetImgGenerate : AVAssetImageGenerator = AVAssetImageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
assetImgGenerate.requestedTimeToleranceAfter = kCMTimeZero;
assetImgGenerate.requestedTimeToleranceBefore = kCMTimeZero;
var error : NSError? = nil
var time : CMTime = CMTimeMakeWithSeconds(fromTime, timescale)
var img : CGImageRef = assetImgGenerate.copyCGImageAtTime(time, actualTime: nil, error: &error)
var frameImg : UIImage = UIImage(CGImage: img)!
return frameImg
}
func sliderValueDidChange(sender:UISlider!) {
var newTime:Float = durInSeconds * sender.value
setFrame(newTime)
}
func setFrame(pos:Float) {
imgCont.image = generateThumnail(urlVideo, fromTime: Float64(pos))
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
self.dismissViewControllerAnimated(true, completion: nil)
videoInfo = info as NSDictionary
urlVideo = videoInfo.objectForKey(UIImagePickerControllerMediaURL) as! NSURL
var asset :AVAsset = AVAsset.assetWithURL(urlVideo) as! AVAsset
vidLength = asset.duration
durInSeconds = Float(CMTimeGetSeconds(vidLength))
timescale = vidLength.timescale
chosePosSlider = UISlider(frame:CGRectMake(20, 450, 340, 20))
chosePosSlider.minimumValue = 0
chosePosSlider.maximumValue = 1
chosePosSlider.continuous = true
chosePosSlider.tintColor = UIColor.greenColor()
chosePosSlider.value = 0
chosePosSlider.addTarget(self, action: "sliderValueDidChange:", forControlEvents: .ValueChanged)
self.view.addSubview(chosePosSlider)
setFrame(0.0)
}