Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用存储文件的路径作为字符串存储和检索图像_Ios_Swift_Image_Filepath_Nsfilemanager - Fatal编程技术网

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

Ios 使用存储文件的路径作为字符串存储和检索图像,ios,swift,image,filepath,nsfilemanager,Ios,Swift,Image,Filepath,Nsfilemanager,我正在使用Realm并将捕获图像的文件路径存储为Strings。我想稍后检索它们以便在tableView中使用。下面是存储每个图像路径的代码: func saveImage(imageName: String){ //create an instance of the FileManager let fileManager = FileManager.default //get the image path thisImagePath = (NSSearchPat

我正在使用Realm并将捕获图像的文件路径存储为
String
s。我想稍后检索它们以便在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
作为文件路径的提到。不知怎的,我忘了那个条目,再也找不到了


任何帮助都将不胜感激

您可以尝试使用以下代码将图像写入文件,而不是创建包含内容的文件<代码>尝试?数据。写入(到: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))
    } }
检索图像的代码看起来不错。如果你在这方面也需要帮助,请评论,我也会发布

希望这能解决你的问题