我正在尝试使用“文件目录”保存图像,但是我无法正确无误地说
没有这样的文件夹。
这是我的代码,我不确定我要去哪里哪里
// Write image to directory
func writeImageToPath(_ path: String, image: UIImage) {
print("Write image to directory")
// get the documents directory url
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let folderURL = documentsURL.appendingPathComponent("upload")
if !FileManager.default.fileExists(atPath: folderURL.path) {
do {
try FileManager.default.createDirectory(atPath: folderURL.path, withIntermediateDirectories: true, attributes: nil)
}
catch {}
}
let fileURL = folderURL.appendingPathComponent(path)
let data = UIImageJPEGRepresentation(image, 0.9)
do {
try data!.write(to: fileURL)
}
catch {}
}
// load image from directory
func loadImageFromPath(_ path: String) -> UIImage? {
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let folderURL = documentsURL.appendingPathComponent("upload")
let fileURL = folderURL.appendingPathComponent(path)
if FileManager.default.fileExists(atPath: fileURL.path) {
//Get Image And upload in server
print("fileURL.path \(fileURL.path)")
do{
let data = try Data.init(contentsOf: fileURL)
let image = UIImage(data: data)
return image
}catch{
print("error getting image")
}
} else {
print("No image in directory")
}
return nil
}
如何正确保存和加载目录中的图像?
答案 0 :(得分:2)
鱼卵,
在大多数项目中,我一直遵循以下代码片段:
要将图像保存在文件夹中,请使用以下功能:
func addImageInFolder(image : UIImage,filename:String){
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let folderURL = documentsURL.appendingPathComponent("upload")
if !FileManager.default.fileExists(atPath: folderURL.path) {
do {
try FileManager.default.createDirectory(atPath: folderURL.path, withIntermediateDirectories: true, attributes: nil)
}
catch {}
}
let fileURL = folderURL.appendingPathComponent(filename)
let data = UIImageJPEGRepresentation(image, 0.9)
do {
try data!.write(to: fileURL)
}
catch {}
}
使用以下功能从存储的文件夹中删除图像:
let fileURL = documentsURL.appendingPathComponent("upload/\(filename ?? "")")
if FileManager.default.fileExists(atPath: fileURL.absoluteString){
do{
try FileManager.default.removeItem(at: fileURL)
}catch{
print("\(sigStr ?? "") delete file delete error")
}
}
要从文件夹获取图像:
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
//TODO: -Get dateTime For get image URL-
let fileURL = documentsURL.appendingPathComponent("upload/\(fileName)")
if FileManager.default.fileExists(atPath: fileURL.path) {
//Get Image And upload in server
print("fileURL.path \(fileURL.path)")
do{
let data = try Data.init(contentsOf: fileURL)
imageView.image = UIImage(data: data)
}catch{
}
}
答案 1 :(得分:0)
Swift 4.2
func saveAndGetImageFromDirectory(_ data: Data, name: String, completionHandler: @escaping (URL?)->()) {
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
var imageURL:URL!
imageURL = documentsURL.appendingPathComponent(name)
do {
try data.write(to: imageURL)
var documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
documentsPath = documentsPath + "/\(name)"
completionHandler(URL.init(fileURLWithPath: documentsPath))
} catch let error {
print(error)
}
}
答案 2 :(得分:-2)
Swift 4:
try this code maybe it will helps you.
//saving image in directory and
let fileManager = FileManager.default
//get the image path
let imagePath = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent(imageName)
//get the image we took from gallery
let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
//get the PNG data for this image
let data = image!.pngData()
//store it in the document directory
fileManager.createFile(atPath: imagePath as String, contents: data, attributes: nil)