从iOS中的图像选择器获取视频url
从照片库中选择视频url时遇到问题。当图像选择器出现时,它无法选择,但我尝试选择它。它能自动压缩图像,不会丢失图像选择器 这是我的密码从iOS中的图像选择器获取视频url,ios,swift,uiimagepickercontroller,Ios,Swift,Uiimagepickercontroller,从照片库中选择视频url时遇到问题。当图像选择器出现时,它无法选择,但我尝试选择它。它能自动压缩图像,不会丢失图像选择器 这是我的密码 self.imagePickerController.sourceType = .savedPhotosAlbum self.imagePickerController.delegate = self self.imagePickerController.mediaTypes = [kUTTypeMovie as! String] self.present(s
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)
}
您可以从
info
获取视频url,例如
videoURL = info[UIImagePickerControllerMediaURL] as? URL
print("videoURL:\(String(describing: videoURL))")
self.dismiss(animated: true, completion: nil)
使用以下代码选择未经压缩的视频:
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)
}
})
}
}
在Swift 4.2中,您必须使用Apple提供的新枚举来捕获视频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
}
您可以阅读关于mediaURL
指定电影的文件系统URL
就这么简单
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)
}
}
}
在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....
}
尝试以下操作:picker.discouse(动画:true,completion:nil)self.present(self.imagePickerController,动画:true,completion:nil),在此代码后,在选择视频或选择视频后打开视频库,视频库自动压缩且pickerview未显示,请勿使用
字符串(描述:)
,它不会做你认为它会做的事。使用一个普通的字符串初始值设定项-或者在这种情况下更好,只需在插值中插入videoURL
。但是由于self.present(self.imagePickerController,动画:true,完成:nil)的原因,此函数不会被调用