Select video from PhotoLibrary in iOS using Swift Select video from PhotoLibrary in iOS using Swift ios ios

Select video from PhotoLibrary in iOS using Swift

Based on Swift 2.2

Suppose you have a imagePickerController and when you want to select both images and videos:

let imagePickerController = UIImagePickerController()var videoURL: NSURL?    @IBAction func selectImageFromPhotoLibrary(sender: UIBarButtonItem) {  imagePickerController.sourceType = .PhotoLibrary  imagePickerController.delegate = self  imagePickerController.mediaTypes = ["public.image", ""]  presentViewController(imagePickerController, animated: true, completion: nil)    }

Then after the video is selected, print out its NSURL.

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {  videoURL = info["UIImagePickerControllerReferenceURL"] as? NSURL  print(videoURL)  imagePickerController.dismissViewControllerAnimated(true, completion: nil)}

For question 2:

Yes, you can do it through AVPlayer, you need import AVKit and AVFoundation, and you code may look like this:

if let videoURL = videoURL{  let player = AVPlayer(URL: videoURL)  let playerViewController = AVPlayerViewController()  playerViewController.player = player  presentViewController(playerViewController, animated: true) {    playerViewController.player!.play()  }}

I made a demo here you can refer, maybe not 100% what you want.

@IBOutlet weak var imgView: UIImageView!var imagePickerController = UIImagePickerController()var videoURL : NSURL?@IBAction func btnSelectVideo_Action(_ sender: Any) {    imagePickerController.sourceType = .savedPhotosAlbum    imagePickerController.delegate = self    imagePickerController.mediaTypes = [kUTTypeMovie as String]    present(imagePickerController, animated: true, completion: nil)}func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {    videoURL = info[UIImagePickerControllerMediaURL]as? NSURL    print(videoURL!)    do {        let asset = AVURLAsset(url: videoURL as! URL , options: nil)        let imgGenerator = AVAssetImageGenerator(asset: asset)        imgGenerator.appliesPreferredTrackTransform = true        let cgImage = try imgGenerator.copyCGImage(at: CMTimeMake(0, 1), actualTime: nil)        let thumbnail = UIImage(cgImage: cgImage)        imgView.image = thumbnail    } catch let error {        print("*** Error generating thumbnail: \(error.localizedDescription)")    }    self.dismiss(animated: true, completion: nil)}

For question 2:

let player = AVPlayer(url: videoURL)let playerController = AVPlayerViewController()playerController.player = playerself.present(playerController, animated: true) {}

Swift 5+ solution:

func openVideoGallery() {    picker = UIImagePickerController()    picker.delegate = self    picker.sourceType = .savedPhotosAlbum    picker.mediaTypes = [""]          picker.allowsEditing = false    present(picker, animated: true, completion: nil)}