【问题标题】:Save image path to core data, then use the path in a eureka row to show the image将图像路径保存到核心数据,然后使用 eureka 行中的路径显示图像
【发布时间】:2023-03-14 12:00:01
【问题描述】:

我正在尝试将图像路径保存到核心数据,以便以后用于在使用 Eureka Forms 创建的行中显示图像:以下代码保存了 URL,但我无法获取文件名。这是当前正在保存的内容。

file:///Users/myName/Library/Developer/CoreSimulator/Devices/21A03F39-026C-417F-8E51-16C5E4BE2FC8/data/Containers/Data/Application/4A7CAE46-5004-4A2E-93A6-A3581F853576/Documents/

@IBAction func btnSave(_ sender: UIButton) {

    UIImageWriteToSavedPhotosAlbum(imageView.image!, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)

     let documentDirectories = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first

    let url = URL(fileURLWithPath: documentDirectories!)

    print ("STARTS HERE \(url) ENDS")

【问题讨论】:

    标签: ios swift eureka-forms


    【解决方案1】:

    尝试使用此代码将图像保存到具有图像名称的文档目录。保存后,您将获得将来使用的文件 URL。

    let documentsDirectoryURL = try! 
    NSFileManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, 
      appropriateForURL: nil, create: true)
    // create a name for your image
    let fileURL = documentsDirectoryURL.URLByAppendingPathComponent("Image1.jpg")
    
    if !NSFileManager.defaultManager().fileExistsAtPath(fileURL.path!) {
        if UIImageJPEGRepresentation(image, 1.0)!.writeToFile(fileURL.path!, atomically: true) {
            print("file saved")
        } else {
            print("error saving file")
        }
    } else {
        print("file already exists")
    }
    print ("STARTS HERE \(fileURL) ENDS")
    

    【讨论】:

    • 感谢您的回复。我正在使用 swift 4 并且有很多东西我必须更新。有一个我无法弄清楚的更新。我收到错误:“URL”类型的值没有成员“URLByAppendingPathComponent”
    • 你可以用这个 swift 4 "let fileURL = documentsDirectoryURL..appendingPathComponent("Image1.jpg")"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-07
    • 2016-03-09
    • 1970-01-01
    • 2014-12-10
    • 2018-07-05
    • 2015-07-14
    • 2012-07-26
    相关资源
    最近更新 更多