我有一个CameraVC课程开头:
class CameraVC: UITableViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var imagePicker = UIImagePickerController()
var image = UIImage()
var videoFilePath = NSURL()
...
我有这个功能:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let mediaType:String = info[UIImagePickerControllerMediaType] as! String
print(mediaType)
if mediaType == "public.image" {
self.image = info[UIImagePickerControllerOriginalImage] as! UIImage
if self.imagePicker.sourceType == UIImagePickerControllerSourceType.Camera {
UIImageWriteToSavedPhotosAlbum(self.image, nil, nil, nil)
}
self.dismissViewControllerAnimated(true, completion: nil)
} else if mediaType == "public.movie" {
self.videoFilePath = info[UIImagePickerControllerMediaURL] as! NSURL
print(self.videoFilePath)
// THIS LINE IS NOT WORK ->
let url = NSURL(string: self.videoFilePath)
/*if self.imagePicker.sourceType == UIImagePickerControllerSourceType.Camera {
if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(self.videoFilePath) {
UISaveVideoAtPathToSavedPhotosAlbum(self.videoFilePath, nil, nil, nil)
}
}*/
}
}
该行:
let url = NSURL(string: self.videoFilePath)
无法正常工作,我有一个红色警告"无法将NSURL类型的值转换为预期的参数类型字符串。
该行:
print(self.videoFilePath)
写入控制台日志:
file:///private/var/mobile/Containers/Data/Application/071BCA9C-D246-4D14-9D56-34057A17079B/tmp/capture-T0x156501280.tmp.y4Ve7u/capturedvideo.MOV
答案 0 :(得分:3)
尝试let url = NSURL(string: self.videoFilePath.absoluteString)
你也意识到self.videoFilePath
已经是NSURL ......
答案 1 :(得分:2)
你的命名很差。 self.videoFilePath不是路径,它是一个url。你在这里做了一个URL:
self.videoFilePath = info[UIImagePickerControllerMediaURL] as! NSURL
答案 2 :(得分:1)
如果您已将self.videoFilePath
作为NSURL
,则不必将其转换为String
,然后NSURL
为了使代码更安全:
if let path = info[UIImagePickerControllerMediaURL] as? NSURL {
self.videoFilePath = path
let url = path
//Continue whatever you want
}