【问题标题】:Swift 3 - save and fetch image from core dataSwift 3 - 从核心数据中保存和获取图像
【发布时间】:2017-02-23 12:53:43
【问题描述】:

是的,我知道还有其他类似的问题,但似乎还没有适合我的答案...

我有一个返回 base64 字符串的 api 调用。我尝试将其保存到核心数据,然后作为图片返回。我不知道我哪里出错了,也许你们中的一些聪明人可以帮助我。

这是我从我的 api 调用中接收数据的地方:

let image = data?.base64EncodedString()
saveImageToDB(brandName: imageBrandName, image: image!)

brandName 只是一个字符串,例如:“Baresso”。

我的 saveImageToDB:

  func saveImageToDB(brandName: String, image: String) {
        let managedContext = getContext()
        let entity = NSEntityDescription.entity(forEntityName: "CoffeeShopImage", in: managedContext)!
        let CSI = NSManagedObject(entity: entity, insertInto: managedContext)


        let image = UIImage(named: image)

        //this is the line that appears to be wrong
        let imageData = NSData(data: (UIImagePNGRepresentation(image!)?.base64EncodedData())!) 

        CSI.setValue(imageData, forKey: brandName)

        do {
            try managedContext.save()
            print("saved!")
        } catch let error as NSError {
            print("Could not save. \(error), \(error.userInfo)")
        }
    }

我的获取函数:

func getImageFromDB(callback: @escaping (_ image: UIImage)-> ()) {

    var imageFromDB: UIImage?

    let fetchRequest: NSFetchRequest<NSManagedObject> = NSFetchRequest(entityName: "CoffeeShopImage")

    do {
        let searchResults = try getContext().fetch(fetchRequest)

        for images in searchResults {
            let image = images.value(forKey: "Baresso") as! NSData
            imageFromDB = UIImage(data: image as Data)!
            callback(imageFromDB!)
        }

    } catch {
        print("Error with request: \(error)")
    }
}

我的错误日志:

fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb) 

看来我没有保存我的图像,这有点道理,但是我不知道我做错了什么?

编辑:

我弄乱了我的类型。更正后的代码如下:

func saveImageToDB(brandName: String, image: NSData) {
    let managedContext = getContext()
    let entity = NSEntityDescription.entity(forEntityName: "CoffeeShopImage", in: managedContext)!
    let CSI = NSManagedObject(entity: entity, insertInto: managedContext)

    CSI.setValue(image, forKey: "image")
    CSI.setValue(brandName, forKey: "brandName")

    do {
        try managedContext.save()
        print("saved!")
    } catch let error as NSError {
        print("Could not save. \(error), \(error.userInfo)")
    }
}

【问题讨论】:

    标签: core-data swift3 uiimage base64


    【解决方案1】:

    你的类型都搞砸了。

    您从data 开始,我假设它是数据并将其转换为名为@9​​87654322@ 的字符串。然后在saveImageToDB 中,您将字符串image 视为UIImage 并尝试首先使用UIImagePNGRepresentation 将其转换为数据(这将失败,因为它是一个字符串)然后您尝试将该数据转换为字符串,然后尝试将该字符串转换回数据,这也是错误的。

    如果您将数据存储在数据库中,只需将数据传递到saveImageToDB。将saveImageToDB 中的image 更改为imageData,并删除所有从字符串到数据再到图像的转换。您从数据开始并希望保存数据,因此没有理由这样做。

    【讨论】:

    • 我确实对我的类型感到困惑。这如何查找我的保存: func saveImageToDB(brandName: String, image: String) { let managedContext = getContext() let entity = NSEntityDescription.entity(forEntityName: "CoffeeShopImage", in: managedContext)! let CSI = NSManagedObject(entity: entity, insertInto: managedContext) CSI.setValue(image, forKey: brandName) do { try managedContext.save() print("saved!") } catch let error as NSError { print("Could not保存。(error), (error.userInfo)") } }
    • CoffeeShopImage 实体定义的属性是什么。我希望有一个叫 imageData 和另一个叫 imageName 。所以你应该有类似CSI.setValue(imageData, forKey: "imageData)CSI.setValue(brandName, forKey: "brandName") 的东西。它们是两个不同的字段,不是键值对。
    • 是的,你是对的。它现在可以工作了,至少可以节省一点。
    猜你喜欢
    • 2017-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多