0

I have imagesDir in myproject dir

MyProject
---- MyProject
     ---- AppDelegate.swift
     ---- Main.storyboard
     ---- ImagesDir
          ---- images1.jpg
          ---- images2.jpg

How can retrive the correct path to "imagesDir"

Is a good idea to save in "imagesDir" the images saved by my application?

Thanks!!!

Stefano Vet
  • 567
  • 1
  • 7
  • 18

2 Answers2

0

You can get path like

let path = NSBundle.mainBundle().pathForResource("images1", ofType: "jpg")

  • thanks @Waseem the code is correct but i need the path of directory for save a new image >> let path = "PATH FOR imagesDIr" imageData.writeToFile(path, atomically: true). Thanks for your help!! – Stefano Vet Oct 20 '14 at 08:34
  • Ok so you want to save image in document directory. var documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String let filePath = documentsPath.stringByAppendingString("/images1.jpg") Then write file at path. – Waseem Ahmad Oct 20 '14 at 10:53
0

In iOS, you can't write data into NSBundle. Instead, write into document directory.

let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0] as String
// documentsDirectory is your app's document path
scottphc
  • 954
  • 7
  • 16
  • thanks @scott work perfectly!. Is a good idea to save many images in documents directory or is there a better solution? For example make a subdir in documents directory ..... – Stefano Vet Oct 20 '14 at 09:37