保存图像然后加载到Swift(iOS) [英] Saving image and then loading it in Swift (iOS)
问题描述
我使用saveImage保存图像。
I am saving an image using saveImage.
func saveImage (image: UIImage, path: String ) -> Bool{
let pngImageData = UIImagePNGRepresentation(image)
//let jpgImageData = UIImageJPEGRepresentation(image, 1.0) // if you want to save as JPEG
print("!!!saving image at: \(path)")
let result = pngImageData!.writeToFile(path, atomically: true)
return result
}
新信息:
保存文件无法正常工作(打印[ - ] ERROR SAVING FILE) -
New info:
Saving file does not work properly ("[-] ERROR SAVING FILE" is printed)--
// save your image here into Document Directory
let res = saveImage(tempImage, path: fileInDocumentsDirectory("abc.png"))
if(res == true){
print ("[+] FILE SAVED")
}else{
print ("[-] ERROR SAVING FILE")
}
为什么saveImage函数没有保存图像?访问权?
Why doesn't the saveImage function save the image? Access rights?
调试信息说:
!!!saving image at: file:///var/mobile/Applications/BDB992FB-E378-4719-B7B7-E9A364EEE54B/Documents/tempImage
然后我使用
fileInDocumentsDirectory("tempImage")
结果是正确的。
然后我使用此路径加载文件
Then I am loading the file using this path
let image = UIImage(contentsOfFile: path)
if image == nil {
print("missing image at: \(path)")
}else{
print("!!!IMAGE FOUND at: \(path)")
}
路径正确,但消息是在...处丢失图像。文件是否以某种方式无法访问或未存储?这种行为可能是什么原因?
The path is correct, but the message is "missing image at..". Is the file somehow inaccessible or not stored? What can be a reason for this behavior?
我正在使用ios 7和带有ios 7模拟器的iphone 5在iphone 4上测试此代码。
I am testing this code on iphone 4 with ios 7 and iphone 5 with ios 7 simulator.
编辑:
1. fileInDocumentsDirectory函数
1. The fileInDocumentsDirectory function
func fileInDocumentsDirectory(filename: String) -> String {
let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let fileURL = documentsURL.URLByAppendingPathComponent(filename).absoluteString
return fileURL
}
推荐答案
Swift 3
此功能会将图像保存在文档文件夹中:
This function will save an image in the documents folder:
func saveImage(image: UIImage) -> Bool {
guard let data = UIImageJPEGRepresentation(image, 1) ?? UIImagePNGRepresentation(image) else {
return false
}
guard let directory = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) as NSURL else {
return false
}
do {
try data.write(to: directory.appendingPathComponent("fileName.png")!)
return true
} catch {
print(error.localizedDescription)
return false
}
}
使用:
let success = saveImage(image: UIImage(named: "image.png")!)
此功能将获得该图像:
This function will get that image:
func getSavedImage(named: String) -> UIImage? {
if let dir = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) {
return UIImage(contentsOfFile: URL(fileURLWithPath: dir.absoluteString).appendingPathComponent(named).path)
}
return nil
}
使用:
if let image = getSavedImage(named: "fileName") {
// do something with image
}
这篇关于保存图像然后加载到Swift(iOS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!