ios - 使用存储文件的路径作为字符串来存储和检索图像

标签 ios swift image filepath nsfilemanager

我正在使用 Realm 并将捕获图像的文件路径存储为 String。我想稍后检索它们以在 tableView 中使用。这是我存储每个图像的路径的代码:

func saveImage(imageName: String){
    //create an instance of the FileManager
    let fileManager = FileManager.default
    //get the image path
    thisImagePath = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent(imageName)
    //get the image taken with camera
    let image = originalCapturedImage
    //get the PNG data for this image
    let data = UIImagePNGRepresentation(image!)
    //store it in the document directory
    fileManager.createFile(atPath: thisImagePath as String, contents: data, attributes: nil)

    print("Picture path at assignment \n")

    print(thisImagePath as Any)

}

这是检索图像的代码:

    ...
var paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
        let documentsPath = paths[0] //Get the docs directory
        let filePath = URL(fileURLWithPath: documentsPath).appendingPathComponent(item.picPath).path
        let image = UIImage(contentsOfFile: filePath)

    print("Picture path at retrieval \n")

    print(item.picPath as Any)


    cell.imageWell.image = image
    cell.imageWell.clipsToBounds = true

    return cell
}

下面是运行时文件路径的比较:

Picture path at assignment 

/var/mobile/Containers/Data/Application/0E9CACAD-C6B3-4F6C-B0DB-72C43AC722E1/Documents/1535219147
...
...
Picture path at retrieval 

/var/mobile/Containers/Data/Application/0E9CACAD-C6B3-4F6C-B0DB-72C43AC722E1/Documents/1535219147

这些路径对我来说看起来相同,但没有出现图像。我搜索了所有内容,有一次提到使用 URL 作为文件路径。不知何故,我失去了该条目的踪迹,并且无法再次找到它。

任何帮助将不胜感激!

最佳答案

您可以尝试使用以下代码将图像写入文件,而不是创建包含内容的文件。 try? data.write(to: URL(fileURLWithPath: documentsPath))

您还可以引用下面的代码来保存图像。

class func saveImageToFileWithDirectory(_ imageData:Data, fileName:String, folderName : String? = nil) {

    let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) as NSArray
    let documentsDirectory = paths.object(at: 0) as! NSString
    let path = documentsDirectory.appendingPathComponent(folderName) as NSString
    if !FileManager.default.fileExists(atPath: path as String) {
        do {
            try FileManager.default.createDirectory(atPath: path as String, withIntermediateDirectories: true, attributes: nil)
        } catch let error as NSError {
            print(error.localizedDescription);
        }
    }
    let imagePath = path.appendingPathComponent(fileName)
    if !FileManager.default.fileExists(atPath: imagePath as String) {
        try? imageData.write(to: URL(fileURLWithPath: imagePath))
    } }

用于检索图像的代码看起来不错。如果您还需要帮助,请发表评论,我也会发布。

希望这能解决您的问题。

关于ios - 使用存储文件的路径作为字符串来存储和检索图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52020452/

相关文章:

ios - Parse orQueryWithSubqueries 无法正常工作(快速)

ios - Swift - 按子值从 Firebase 中选择

ios - 返回 UITableViewCell 自定义单元格

ios - 如何使用UIDocumentBrowserViewController在iCloud上重新打开UIDocument

ios - Facebook Graph API GET 请求 - 应包含 "fields"参数(Swift,Facebook SDK v4.5.1)

ios - 当使用 JSON 在后台加载数组并在 TableView 中进行选举时,事件指示器不会消失

html - 是否可以将二进制图像数据放入 html 标记中,然后在任何浏览器中照常显示图像?

css - 淡入淡出 2 个不透明度为 0.7 的图像

ios - 如何解决 Flurry Error code -104 No render time ad received for this space?

Android:如何从应用程序将图像作为电子邮件附件发送?