[ create a new paste ] login | about

Link: http://codepad.org/xjuaRgPI    [ raw code | fork ]

cam - Plain Text, pasted on Mar 28:
  // Do any additional setup after loading the view, typically from a nib.
        
       //  let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
      //   print("document directory path : \(documentsPath)")
      //  https://iosdevcenters.blogspot.com/2016/04/save-and-get-image-from-document.html
        
        //---------
        
        let fileManager = NSFileManager.defaultManager()
        let paths = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString).stringByAppendingPathComponent("Arrow_grey.png")
        let image = UIImage(named: "Arrow_grey.png")
        print(paths)
        let imageData = UIImageJPEGRepresentation(image!, 0.5)
        fileManager.createFileAtPath(paths as String, contents: imageData, attributes: nil)
        
        //------- writing and reading text file
        
        
        let file = "file.txt" //this is the file. we will write to and read from it
        
        let text = "some text" //just a text
        
        if let dir = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true).first {
            let path = NSURL(fileURLWithPath: dir).URLByAppendingPathComponent(file)
            
            //writing
            do {
                try text.writeToURL(path, atomically: false, encoding: NSUTF8StringEncoding)
            }
            catch {/* error handling here */}
            
            //reading
            do {
                let text2 = try NSString(contentsOfURL: path, encoding: NSUTF8StringEncoding)
            }
            catch {/* error handling here */}
        }



Create a new paste based on this one


Comments: