Tuesday 11 November 2014

Swift + Save and Load Image From DocumentDirectory

var selectedImage: UIImage = "image.png"
            
  
let fileManager = NSFileManager.defaultManager()
            
var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
            
var filePathToWrite = "\(paths)/SaveFile.png"
            
var imageData: NSData = UIImagePNGRepresentation(selectedImage)
            
fileManager.createFileAtPath(filePathToWrite, contents: imageData, attributes: nil)
            
var getImagePath = paths.stringByAppendingPathComponent("SaveFile.png")
            
            
            
if (fileManager.fileExistsAtPath(getImagePath))
{
       println("FILE AVAILABLE");

       //Pick Image and Use accordingly
       var imageis: UIImage = UIImage(contentsOfFile: getImagePath)!
                
       let data: NSData = UIImagePNGRepresentation(imageis)
                
}
else
{
      println("FILE NOT AVAILABLE");

}

8 comments:

  1. Hey Thanks for posting all of these swift examples, they are very helpful.

    ReplyDelete
  2. I have modified the code to support swift 2

    // Write to Documents Directory

    let selectedImage: UIImage = profilePictureView.image!

    let fileManager = NSFileManager.defaultManager()

    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String

    let filePathToWrite = "\(paths)/User_Profile_Image.jpg"

    // let imageData: NSData = UIImagePNGRepresentation(selectedImage)!
    let jpgImageData = UIImageJPEGRepresentation(selectedImage, 1.0)

    fileManager.createFileAtPath(filePathToWrite, contents: jpgImageData, attributes: nil)

    // Check file saved successfully
    let getImagePath = (paths as NSString).stringByAppendingPathComponent("User_Profile_Image.jpg")
    if (fileManager.fileExistsAtPath(getImagePath))
    {
    print("FILE AVAILABLE");

    //Pick Image and Use accordingly
    // let imageis: UIImage = UIImage(contentsOfFile: getImagePath)!

    // let data: NSData = UIImagePNGRepresentation(imageis)

    }
    else
    {
    print("FILE NOT AVAILABLE");

    }
    // End

    ReplyDelete
  3. i want to write csv file to document directory. Can u guide me in that.

    ReplyDelete
  4. how to pass the image to php server in swift 2

    ReplyDelete
  5. helped me out when other ways failed - thanks!

    ReplyDelete
  6. how to video and image parth store

    ReplyDelete
  7. Hi,

    I'm Niall Kehoe and I'm 12 years old. I'm the youngest iOS App developer in Europe. I'm trying to save an image with labels on top using this example. Unfortunately, when it saves an image it only saves the image you specified How can I get it to save with labels on top using this example?

    I am saving using .writetoFile using different code. But it only saves in .pdf and when I try to recover it I can't : http://stackoverflow.com/questions/35560443/recovering-images-from-write-to-file-in-swift?noredirect=1#comment58810079_35560443 .

    Image here: http://postimg.org/image/6m74hwtaj/39486ef0/
    Please Help!

    ReplyDelete
  8. Lucky Club Casino site
    【ZALO:945084771】 ,casino site review & rating 【ZALO:945084771】,casino site review luckyclub.live & rating

    ReplyDelete