如何在CoreData中保存和获取JSON Image URL以便脱机以在Swift中的tableView中显示

时间:2018-06-07 13:32:19

标签: json uitableview core-data swift3 uiimageview

在JSON的下面代码中,我能够在标签中保存和获取文本值,但我无法在tableView中显示图像。请帮我将String转换为NSData并将NSData转换为String。

这是JSON

{
"actors": [
{
  "name": "Brad Pitt",
  "description": "William Bradley 'Brad' Pitt is an American actor and film producer. He has received a Golden Globe Award, a Screen Actors Guild Award, and three Academy Award nominations in acting categories",
  "dob": "December 18, 1963",
  "country": "United States",
  "height": "1.80 m",
  "spouse": "Jennifer Aniston",
  "children": "Shiloh Nouvel Jolie-Pitt, Maddox Chivan Jolie-Pitt",
  "image": "http://microblogging.wingnity.com/JSONParsingTutorial/brad.jpg"
}, ....

这里是如何在CoreData中保存图片网址并获取以在tableView中显示图片

这是struct

struct JsonData {

var nameS: String?
var picS: String?
var spouseS: String?

    init(name: String, image: String, spouse: String) {

    self.nameS = name
    self.picS = image
    self.spouseS = spouse
}
}

为了保存,我确实喜欢这样。如果这是错误的,请建议我如何保存

 let actors = jsonObj["actors"] as! [[String: Any]]
   for actor in actors {
       let name = actor["name"] as! String
       let pic = actor["image"] as! String
       let spouse = actor["spouse"] as! String

        let coreData = NSEntityDescription.insertNewObject(forEntityName: "Actor", into: self.context) as! Actor
                coreData.name = name
                coreData.spouse = spouse

                //Converting NSString to NSData
                let data = pic.data(using: .utf8)

                //assigning NSData value to core data attribute
                coreData.image = data! as NSData//picNSData as NSData
                print("core data saved images \(coreData.image!)")
            }

在抓取时我试过这样的

   func fetchData() {
    do {
        let fetchRequest : NSFetchRequest<Actor> = Actor.fetchRequest()
        let actors = try context.fetch(fetchRequest)

        if actors.count > 0 {

            for actor in actors as [Actor] {
                let nameCore = actor.value(forKey: "name")
                let spouseCore = actor.value(forKey: "spouse")

                //here imageData contains nsdata
                let imageData = actor.value(forKey: "image")
                //converting NSData to String for append JsonData Struct
                let imageCore = String(describing: imageData!)
                print("we r in fetch image.. \(imageCore)")

                self.iteamsArray.append(JsonData(name: nameCore as! String, image: imageCore , spouse: spouseCore as! String))
            }
            DispatchQueue.main.async {
                self.tableView.reloadData()
            }
        }
        else {
            downloadJsonwithImages()
        }
    } catch { print(error) }
}

这里downloadJsonwithImages()将在线显示tableview

这是cellForRowAt

 let cell = tableView.dequeueReusableCell(withIdentifier: "JsonCell", for: indexPath) as! JsonTableViewCell

    let aData = iteamsArray[indexPath.row]
    cell.labelName.text = aData.nameS
    cell.labelUsername.text = aData.spouseS

    cell.picImageview.setImageFromURl(stringImageUrl: aData.picS!)
    //in online getting images in tableview 
    print("in cell \(aData.picS)")//while offline it contains image data but not getting in tableview

    cell.selectionStyle = .none
    return cell

用于下载我用过的图像,我在项目中也安装了sdwebimage pod

// MARK: - UIImageView
extension UIImageView{

func setImageFromURl(stringImageUrl url: String){

    if let url = NSURL(string: url) {
        if let data = NSData(contentsOf: url as URL) {

            DispatchQueue.main.async {
                self.image = UIImage(data: data as Data)
            }
        }
    }
}
}

请帮我在离线时在tableview中显示图片。

0 个答案:

没有答案