从所选视频中获取图像UIImagePickerController

时间:2015-11-16 05:31:10

标签: ios swift uiimagepickercontroller viewcontroller

在用户选择视频后,如何检索或制作要在另一个视图控制器中使用的所选视频的缩略图?

 func imagePickerController1(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
        // 1
        let mediaType = info[UIImagePickerControllerMediaType] as! NSString
        // 2
        dismissViewControllerAnimated(true) {
            // 3
            if mediaType == kUTTypeMovie {
                let moviePlayer = MPMoviePlayerViewController(contentURL: info[UIImagePickerControllerMediaURL] as! NSURL)
                self.presentMoviePlayerViewControllerAnimated(moviePlayer)
            }

        }

        let destination = self.storyboard!.instantiateViewControllerWithIdentifier("FinializePost")
        self.navigationController!.pushViewController(destination, animated: true);
    }

1 个答案:

答案 0 :(得分:0)

您可以使用此代码:

 let URL_originalvideo = info[UIImagePickerControllerMediaURL]

    let asset: AVAsset = AVAsset.assetWithURL(URL_originalvideo) as AVAsset
    let imageGenerator = AVAssetImageGenerator(asset: asset);
    let time = CMTimeMakeWithSeconds(1.0, 1)

    var actualTime : CMTime = CMTimeMake(0, 0)
    var error : NSError?
    let myImage = imageGenerator.copyCGImageAtTime(time, actualTime: &actualTime, error: &error)

myImage就是你所需要的。