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

编程入门 行业动态 更新时间:2024-10-26 19:31:48
本文介绍了如何使用Swift 3和Alamofire从JSON下载图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

How to download image from JSON using Alamofire and Swift 3? I am getting dictionary of data. See the following JSON response. I am able to print data in labels but I can't download the image. This is the response I am getting from the API.

userJson userJson userJson userJson [status:1,student:{ admission_date=2017/06/14; admission_no= 13538; class_teacher=Caroline Forbes; dob = 04/05/2001; email =ranisagar.sivadas@gmail; father_name= SAGAR SIVADAS;性别=男性; image = /system/images/86/j1f9DiJi_medium.jpg?1504593436; mother_name= RANI R S; name =Abhijith Sagar;电话= 9066260799;宗教= 印度教; school_email=13538.861@demo.in; student_id= 86; }, message:成功获取详细信息。]

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"; "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,您就可以下载图像。如果不是预期的答案,请重新编辑您的问题。

Have a look at below code you just need to pass a link url to alamofire and you can download a image . if its not the expected answer please Re edit your question .

let strURL1:String = "www.planwallpaper/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";

这是一个有效的网址或路径,如果它是一个网址,你需要添加你的基本网址+您的/system/images/86/j1f9DiJi_medium.jpg?1504593436并将其传递给alamofire块以下载图片,如上例所示

is this a valid url or path if its a url you need to add your base url + your " /system/images/86/j1f9DiJi_medium.jpg?1504593436" and pass it to alamofire block to download image as in my above example

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

希望它有所帮助。

更多推荐

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

本文发布于:2023-11-26 15:31:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634278.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   图像   Alamofire   Swift   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!