gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 12:16:56 25 4
gpt4 key购买 nike

如何使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com