本文介绍了保存图像,然后在 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 的 iphone 4 和使用 ios 7 模拟器的 iphone 5 上测试此代码.

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
}

推荐答案

此功能将在文档文件夹中保存图像:

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) 中加载它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 13:25