Search code examples
iosswiftswift3

How to convert video (in gallery) to NSData? in Swift


I just haven't "info" in Swift imagePickerController so I don't know how get url and convert it to data to send to web-service.

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {

  var videoDataURL = info[UIImagePickerControllerMediaURL] as! NSURL!
  var videoFileURL = videoDataURL.filePathURL
  var video = NSData.dataWithContentsOfMappedFile("\(videoDataURL)")
}

Solution

  • Xcode 10 • Swift 4.2 or later

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        if let url = info[.mediaURL] as? URL {
            do {
                try FileManager.default.moveItem(at: url, to: documentsDirectoryURL.appendingPathComponent("videoName.mov"))
                print("movie saved")
            } catch {
                print(error)
            }
        }
    }
    

    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)
            }
        }
    }
    

    Swift 2

    You should use if let to unwrap your optionals. Also NSData.dataWithContentsOfMappedFile was deprecated iOS8. Try using NSData method initializer contentsOfURL:

    Note: You need also to change the didFinishPickingMediaWithInfo declaration from [NSObject : AnyObject] to [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)
            }
        }
    }
    

    as mentioned by Rob the data can be really large but instead of copying the file you should move the file to the documents folder as follow:

    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)
        }
    }