ios - 如何从 Kingfisher 将图像作为 NSData 写入 Realm?

标签 ios swift xcode realm kingfisher

我在 swift 2.3 中使用 Realm 数据库Kingfisher 框架 来获取图像。 我通过 JSON

从服务器获取图像
let urls = NSURL(fileURLWithPath: newsRealm.newsImage)
let prefetcher = ImagePrefetcher(urls: [urls], optionsInfo: nil, progressBlock: nil, completionHandler: {
(skippedResources, failedResources, completedResources) -> () in
print("These resources are prefetched: \(completedResources)")
})
prefetcher.start()

'成功了,但是Realm数据库中的图片是NSData,如何将接收到的Kingfisher数据写入Realm 作为 NSData

Realm 类:

class News: Object {
  dynamic var newsImage: String = ""
  dynamic var images: NSData?
}

嗨!我无法将图像写入我的数据库:

let urls = NSURL(fileURLWithPath: newsRealm.newsImage)
let prefetcher = ImagePrefetcher(urls: [urls], optionsInfo: nil, progressBlock: nil, completionHandler:
{(skippedResources, failedResources, completedResources) -> () in
print("These resources are prefetched: (completedResources.description)")
let images: [NSData] = completedResources
newsRealm.images = images

最佳答案

使用 optionsInfo: [.TargetCache(myCache)] 设置缓存 url,您可以在其中检索数据(如 here 所述), 或使用 example 检索图像 检索可以转换为 NSDataUIImage,如 here .

关于ios - 如何从 Kingfisher 将图像作为 NSData 写入 Realm?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40192992/

相关文章:

ios - 无法更新 Realm 对象

ios - xcode: xcode 11.2 中的 iOS 10 及以下模拟器

swift - 为什么我不能关闭 Swift 中的 replaykit previewController?

ios - 为测试创建 Alamofire 响应

swift - runAction() 内的变化值没有改变

iphone - UIScrollView 中的动画

ios - JSON 解码器类型不匹配错误

c++ - .mm 测试文件的 OCMock 3.0.2 链接器错误

ios - AVAudioRecorder 录制 AAC/m4a

ios - 将自定义参数传递给 Phonegap 插件