1

我做应用程序swift 2.3Realm database获取Kingfisher framework图像。我从服务器获取图像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 databaseNSData,如何将接收到的数据写入KingfisherRealmNSData

领域类:

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
4

2 回答 2

1

用于optionsInfo: [.TargetCache(myCache)]设置缓存 url,您可以在其中检索数据(如此处所述),或使用示例 检索可以转换为like here的示例 retrieveImageUIImageNSData

于 2016-10-22T14:18:04.777 回答
0

completedResources不是一个数组NSDatacompletedResources是一个数组ResourceResource持有cacheKeydownloadURL。您应该使用从缓存中检索图像cacheKey,然后将图像转换为NSData. 然后将数据存储到Realm。

let images: [NSData] = completedResources.map { (resource) -> NSData in
    let image = ImageCache.default.retrieveImageInDiskCache(forKey: resource.cacheKey)!
    return UIImagePNGRepresentation(image)
}
于 2016-10-27T13:12:56.450 回答