ios - 如何使用 Swift 3 和 Alamofire 从 JSON 下载图像?

标签 ios json swift image alamofire

如何使用 Alamofire 和 Swift 3 从 JSON 下载图像?我正在获取数据字典。请参阅以下 JSON 响应。我可以打印标签中的数据,但无法下载图像。这是我从 API 获得的响应。

userJson userJson userJson userJson ["status": 1, "student": { "admission_date" = "14/06/2017"; "admission_no" = 13538; "class_teacher" = "Caroline Forbes"; dob =
"04/05/2001"; email = "ranisagar.sivadas@gmail.com"; "father_name" = "SAGAR SIVADAS"; gender = Male; image =
"/system/images/86/j1f9DiJi_medium.jpg?1504593436"; "mother_name" = "RANI R S"; name = "Abhijith Sagar"; phone = 9066260799; religion = Hindu; "school_email" = "13538.861@demo.in"; "student_id" = 86; },
"message": Details fetched successfully.]

这是我的代码。

func SetUpUIProfiledata() {
    APIManager.sharedInstance.getParentDataFromURL(){(userJson)-> Void in
        let swiftyJsonVar = JSON(userJson)
        print("userJson userJson userJson userJson",userJson)
        print("swiftyJsonVar",swiftyJsonVar)
        let message = swiftyJsonVar["message"].rawString()
        let sudent = swiftyJsonVar["student"].rawString()!
        let jsonData = sudent.data(using: .utf8)!
        let dictionary = try? JSONSerialization.jsonObject(with: jsonData, options: []) as! Dictionary<String, Any>
        self.name_lbl.text = dictionary?["name"] as? String
        self.fatherName_lbl.text = dictionary?["father_name"] as? String
        self.motherName_lbl.text = dictionary?["mother_name"] as? String
        self.phone_lbl.text = dictionary?["phone"] as? String
        self.email_lbl.text = dictionary?["email"] as? String
        self.dob_lbl.text=dictionary?["dob"] as? String
        self.gender_lbl.text=dictionary?["gender"] as? String
        self.religion_lbl.text=dictionary?["religion"] as? String
        self.admissionDate_lbl.text=dictionary?["admission_date"] as? String
        self.admissionNum_lbl.text=dictionary?["admission_no"] as? String
        self.schoolMail_lbl.text=dictionary?["school_email"] as? String
        self.teacher_lbl.text=dictionary?["class_teacher"] as? String
    }
}

最佳答案

看看下面的代码,你只需要将链接 url 传递给 alamofire,你就可以下载图像。如果不是预期的答案,请重新编辑您的问题。

       let strURL1:String = "https://www.planwallpaper.com/static/images/9-credit-1.jpg"
Alamofire.request(strURL1).responseData(completionHandler: { response in
    debugPrint(response)

    debugPrint(response.result)

    if let image1 = response.result.value {
        let image = UIImage(data: image1)
         self.imageView.image = image

    }
})

我查看了您提供的输出

 image =
"/system/images/86/j1f9DiJi_medium.jpg?1504593436";

这是一个有效的 url 或路径,如果它是一个 url,你需要添加你的 base url + 你的“/system/images/86/j1f9DiJi_medium.jpg?1504593436” 并将其传递给 alamofire block 下载图片 如我上面的例子

"https://www.planwallpaper.com" - baseURl
"static/images/9-credit-1.jpg" - image Path as in your case

希望对您有所帮助。

关于ios - 如何使用 Swift 3 和 Alamofire 从 JSON 下载图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46168008/

相关文章:

ios - UIStackView - 三个排列的具有相对宽度的 subview

ios - 使用自定义 CSS 类从 HTML 中使用属性字符串填充单元格标签

jQuery读取并输出JSON数据

ios - 将数据库保存在 AppGroup 的共享容器中是一种好方法吗

swift - 如何查找可导入的函数 [Swift]

objective-c - GTMOAuth 的 FatSecret REST KIT 问题

ios - 如何在iOS应用程序上检索测验问题并打印结果?

php - 删除php中仅一个json键值对中的多余反斜杠

java - 如何包装 jackson 序列化异常

swift - 当文件未保存在 Xcode 中时消除文件图标阴影