mohit.dnb
mohit.dnb

Reputation: 41

Getting video url from image picker in iOS

I'm facing a problem selecting the video url from the photo gallery. When the image picker is presented, it can not choose but I try to choose it. It automatically compressing and image picker is not dissmissed.

This is my code.

self.imagePickerController.sourceType = .savedPhotosAlbum
self.imagePickerController.delegate = self
self.imagePickerController.mediaTypes = [kUTTypeMovie as! String]

self.present(self.imagePickerController, animated: true, completion: nil)


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

    imagePickerController.dismiss(animated: true, completion: nil)
}

Upvotes: 3

Views: 9559

Answers (5)

CodeBender
CodeBender

Reputation: 36620

In Swift 4.2, you must use a new enum provided by Apple to capture the video URL:

// Using the full key
if let url = info[UIImagePickerController.InfoKey.mediaURL] as? URL {
    // Do something with the URL
}

// Using just the information key value
if let url = info[.mediaURL] as? URL {
    // Do something with the URL
}

You can read about mediaURL here.

Specifies the filesystem URL for the movie.

Upvotes: 7

Shubham Narang
Shubham Narang

Reputation: 534

Use below code for Select the video without compression :

func imagePickerController(_ picker: UIImagePickerController,
                             didFinishPickingMediaWithInfo info: [String : Any]) {

    let mediaType = info[UIImagePickerControllerMediaType] as? String
    let videoString = kUTTypeVideo as? String
    let movieString = kUTTypeMovie as? String

    if (mediaType == videoString) || (mediaType == movieString) {
      var videoRef = info[UIImagePickerControllerReferenceURL] as? URL
      var refResult = PHAsset.fetchAssets(withALAssetURLs: [videoRef], options: nil) as? PHFetchResult
      var videoRequestOptions = PHVideoRequestOptions()
      videoRequestOptions.version() = .original
      PHImageManager.default().requestAVAsset(forVideo: refResult.firstObject as? PHAsset ?? PHAsset(), options: videoRequestOptions, resultHandler: {(_ asset: AVAsset, _ audioMix: AVAudioMix, _ info: [AnyHashable: Any]) -> Void in
        if (asset is AVURLAsset) {
          var originURL: URL? = (asset as? AVURLAsset)?.url
          // Now you have the URL of the original video.
          picker.dismiss(animated: true, completion: nil)
        }
        else
        {
          picker.dismiss(animated: true, completion: nil)
        }
      })
    }
  }  

Upvotes: -1

nitin.agam
nitin.agam

Reputation: 2142

In Swift 4.2:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    picker.dismiss(animated: true, completion: nil)
    guard let mediaURL = info[UIImagePickerController.InfoKey.mediaURL] as? URL else { return }
    // You can do whatever youo want here....
}

Upvotes: 1

ngbaanh
ngbaanh

Reputation: 535

Just simple like this.

extension HomeVC: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        let videoUrl = info[UIImagePickerControllerMediaURL] as! URL
        // Do something with the videoUrl

        DispatchQueue.main.async { // Dismiss it, remember using `picker`
            picker.dismiss(animated: true, completion: nil)
        }
    }
}

Upvotes: 0

Ketan Parmar
Ketan Parmar

Reputation: 27438

You can get video url from info like,

videoURL = info[UIImagePickerControllerMediaURL] as? URL
print("videoURL:\(String(describing: videoURL))")
self.dismiss(animated: true, completion: nil)

Upvotes: 1

Related Questions