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

I just don’t have “information” in the Swift imagePickerController, so I don’t know how to get the URL and convert it to data for sending to the 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)")
}
+3
source share
2 answers

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 it if you are allowed to deploy your options. In addition, NSData.dataWithContentsOfMappedFileiOS8 is deprecated. Try using the initializer value of the NSDataOfURL method:

Note. You also need 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)
        }
    }
}

Rob, , :

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

:

  • :

    var videoDataURL = info[UIImagePickerControllerMediaURL] as! NSURL!
    

    info[UIImagePickerControllerMediaURL] ( , nil, ), NSURL!. . ( NSURL, NSURL!):

    if let videoDataURL = info[UIImagePickerControllerMediaURL] as? NSURL { ... }
    
  • filePathURL:

    var videoFileURL = videoDataURL.filePathURL
    

    URL- , , , videoDataURL. , path:

    let videoPath = videoDataURL.path
    

    , Apple , videoDataURL path, filePathURL.

  • dataWithContentsOfMappedFile:

    var video = NSData.dataWithContentsOfMappedFile("\(videoDataURL)")
    

    dataWithContentsOfMappedFile, Swift:

    let video = NSData(contentsOfMappedFile: videoPath!)
    

    dataWithContentsOfMappedFile , :

    let video = try NSData(contentsOfFile: videoPath!, options: .DataReadingMappedIfSafe)
    

    , videoPath, :

    let video3 = try NSData(contentsOfURL: videoDataURL, options: .DataReadingMappedIfSafe)
    

    , try do catch.

  • , , let, .

-

, NSData . NSFileManager, . , , .

, :

if let videoDataURL = info[UIImagePickerControllerMediaURL] as? NSURL {
    do {
        // build your destination URL however you want
        //
        // let tempFolder = NSURL(fileURLWithPath: NSTemporaryDirectory())
        // let destinationURL = tempFolder.URLByAppendingPathComponent("test.mov")

        // or 

        let documents = try NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: false)
        let destinationURL = documents.URLByAppendingPathComponent("test.mov")

        // but just copy from the video URL to the destination URL

        try NSFileManager.defaultManager().copyItemAtURL(videoDataURL, toURL: destinationURL)
    } catch {
        print(error)
    }
}

-, NSURLSessionUploadTask, . , , , : , , , , NSData , .

+3

All Articles