How to save image or video from UIPickerViewController to document directory?

save file to documents directory ios swift 4
get video from document directory ios swift
ios save file to documents directory
get file from document directory ios swift
swift copy file to documents directory
how to save image in document directory in swift 4
save image to photo library swift 4
getdocumentsdirectory
  1. Handle selected image or video:

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        print("ok")
    
        if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
            //what to do to save that image
        } else {
            //how to get the video and save
        }
    }
    
  2. Save it to the document directory:

    let path = try! NSFileManager.defaultManager().URLForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomain: NSSearchPathDomainMask.UserDomainMask, appropriateForURL: nil, create: false)
    let newPath = path.URLByAppendingPathComponent("image.jpg") //or video.mpg for example
    

How to save that image to following newPath?

Take, save and retrieve a photo — iOS #13, We save it to the Documents Directory, which is part of your app where you can save .. things like images! Add this function to the ViewController. I am using UIPickerViewController to capture photo, but I am getting layout as below: As you can see in image - The Switch camera button is not showing properly. Below is my code ios objective-c uipickerviewcontroller

Updating the accepted answer to Swift 3 in Xcode 8.3.3

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        let path = try! FileManager.default.url(for: FileManager.SearchPathDirectory.documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask, appropriateFor: nil, create: false)
        let newPath = path.appendingPathComponent("image.jpg")
        let jpgImageData = UIImageJPEGRepresentation(image, 1.0)
        do {
            try jpgImageData!.write(to: newPath)
        } catch {
            print(error)
        }

    } else {
        let videoURL = info[UIImagePickerControllerMediaURL] as! NSURL
        let videoData = NSData(contentsOf: videoURL as URL)
        let path = try! FileManager.default.url(for: FileManager.SearchPathDirectory.documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask, appropriateFor: nil, create: false)
        let newPath = path.appendingPathComponent("/videoFileName.mp4")
        do {
            try videoData?.write(to: newPath)
        } catch {
            print(error)
        }
    }
}

UIImagePickerController, An image picker controller manages user interactions and delivers the results of or saved image or movie, or cancels the operation, dismiss the image picker� Today i am explaining how to upload image in mvc and save the image in project folder. Create a new MVC project, and choose the following template. Here is my solution explorer of the project.

Swift 5

func getDocumentsDirectory() -> URL {
        let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        return paths[0]
    }

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        picker.dismiss(animated: true)
        guard let image = info[.editedImage] as? UIImage else {
            print("No image found")
            return
        }
        // saving to application directory
        let imageName = UUID().uuidString
        let imagePath = getDocumentsDirectory().appendingPathComponent(imageName)
        if let jpegData = image.jpegData(compressionQuality: 0.8) {
            print("Image Save to path \(imagePath)")
            try? jpegData.write(to: imagePath)
        }
     }

How to Programmatically Save and Load UIImage Files in The , The app will fetch some pictures using the UIImagePickerController, it will then save those images, precisely ,in a folder inside the Document� I am trying to select a video from UIPickerViewController, compress the video into about 1.5MB, and post it to a server. I am already able to select an image and post successfully. I am also able to select a video and post it successfully. The problem is that since the video file is about 27MB, it is too big to playback once sent.

Save and Get Image from Document Directory in Swift – iPhone , Every Apps have its own storage at Document directory. In Document directory user can store audio, video, image, pdf and others files. You can� Syntax: Image.save(fp, format=None, **params) Parameters: fp – A filename (string), pathlib.Path object or file object. format – Optional format override. If omitted, the format to use is determined from the filename extension. If a file object was used instead of a filename, this parameter should always be used.

Save and Get Image from Document Directory in Swift, In Document directory user can store audio,video,image,pdf and others files without collide to the others apps. and you can also read and write data of particular� When you open the file in Notepad, or in your favorite text editor or word processor, you’ll see the same directory listing you would see had you just used the dir command alone at the prompt. If you prefer just a list of the filenames themselves, you can modify the previous command with the /b switch:

How to Use UIImagePickerController for a Camera and Photo , Right-click and save the image below: pizza. Click on Assets.Xcassets and drag the pizza file into the assets folder. Go back to the story board� If you don’t need to edit the image before saving it to your hard drive, you can easily take the screenshot and save it as an image file by pressing a single key combination. Here’s how: 1 – Press the Windows key and the Print Screen key at the same time.

Comments
  • Can I do the similar thing for video? please update your answer with video example.
  • yes @BartłomiejSemańczyk it can be, i will update my answer. but you need to use video picker and its delegate for the same. same delegate method will not work.
  • the last thing, how do we know what extension is that video? Can we differ that casting it from mediaInfo?
  • it will be .mp4 video only if its stored in iOS device, though you can find it from UIImagePickerControllerMediaURL in mediaInfo dictionary.