我只是在Swift imagePickerController中没有“信息”,所以我不知道如何获取url并将其转换为数据以发送到Web服务。
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { var videoDataURL = info[UIImagePickerControllerMediaURL] as! NSURL! var videoFileURL = videoDataURL.filePathURL var video = NSData.dataWithContentsOfMappedFile("\(videoDataURL)") }
Xcode 8.3•Swift 3.1
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]) { let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! if let fileURL = info[UIImagePickerControllerMediaURL] as? URL { do { try FileManager.default.moveItem(at: fileURL, to: documentsDirectoryURL.appendingPathComponent("videoName.mov") print("movie saved") } catch { print(error) } } }
迅捷2
您应该使用if来拆开可选组件。NSData.dataWithContentsOfMappedFileiOS8 也已弃用。尝试使用NSData方法初始值设定项contentsOfURL:
NSData.dataWithContentsOfMappedFile
注意:您还需要将didFinishPickingMediaWithInfo声明从更改[NSObject : AnyObject]为[String : AnyObject]
[NSObject : AnyObject]
[String : AnyObject]
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { if let fileURL = info[UIImagePickerControllerMediaURL] as? NSURL { if let videoData = NSData(contentsOfURL: fileURL) { print(videoData.length) } } }
正如Rob所提到的,数据可能确实很大,但是除了复制文件之外,您还应该按照以下步骤将文件移动到documents文件夹:
let documentsDirectoryURL = try! NSFileManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true) if let fileURL = info[UIImagePickerControllerMediaURL] as? NSURL { do { try NSFileManagerdefaultManager().moveItemAtURL(fileURL, toURL: documentsDirectoryURL.URLByAppendingPathComponent("videoName").URLByAppendingPathExtension("mov")) print("movie saved") } catch { print(error) } }