Reputation: 57
I want to save images to documents/images/AET/1.jpg but so far I was able to save to documents folder only.
I wrote code
let fileManager = FileManager.default
let paths = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent("images")
try! fileManager.createDirectory(atPath: paths, withIntermediateDirectories: true, attributes: nil)
fileManager.changeCurrentDirectoryPath(paths)
let image = imagePic// UIImage(named: "apple.jpg")
print(paths)
let paths3 = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent("/\(chkString!).jpg")
print("chkString=\(chkString!)")
if !fileManager.fileExists(atPath: paths){
try! fileManager.createDirectory(atPath: paths3, withIntermediateDirectories: true, attributes: nil)
}else{
print("Already dictionary created.")
}
let imageData = UIImageJPEGRepresentation(image, 0.5)
// let pathToDatabase = paths3.appending("/\(chkString)")
// let pathToDatabase = paths3.appendingPathComponent("/\(chkString)")
fileManager.createFile(atPath: paths3 as String, contents: imageData, attributes: nil)
print("imagePath = \(paths3)")
Upvotes: 1
Views: 3087
Reputation: 285150
The main issue is that you cannot create a directory passing a file path including the file name (/../AET/1.jpg
). You need to specify a folder path without the file name.
Generally it's highly recommended to use the URL related API.
This example uses the URL related API, more descriptive variable names and the API of Data
to write the image data to disk.
let chkString : String? = "AET"
let fileManager = FileManager.default
do {
let documentDirectoryURL = URL.documentsDirectory
let imagesFolderURL = documentDirectoryURL.appendingPathComponent("images/\(chkString!)")
let imageURL = imagesFolderURL.appendingPathComponent("1.jpg")
let image = imagePic// UIImage(named: "apple.jpg")
if !fileManager.fileExists(atPath: imagesFolderURL.path){
try fileManager.createDirectory(at: imagesFolderURL, withIntermediateDirectories: true, attributes: nil)
} else {
print("Already dictionary created.")
}
let imageData = image.jpegData(compressionQuality: 0.5)
try imageData!.write(to: imageURL)
print("imagePath =", imageURL.path)
} catch {
print(error)
}
Upvotes: 5
Reputation: 742
let fileManager = FileManager.default
let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let path = "\(documentPath[0])/images/AET"
if !fileManager.fileExists(atPath: path) {
try! fileManager.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil)
} else {
print("Directroy is already created")
}
You can print the path and check through terminal it will give the directory that you want.
Upvotes: 3
Reputation: 3682
Use this:
let fileManager = FileManager.default
let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
print(documentPath)
let imageFolder = documentPath.appending("/images/AET")
if !fileManager.fileExists(atPath: imageFolder) {
do{
try fileManager.createDirectory(atPath: imageFolder, withIntermediateDirectories: true, attributes: nil)
}
catch (let error){
print("Failed to create Directory: \(error.localizedDescription)")
}
}
You should create subfolders like "/images/AET".
Upvotes: 1