如何使用Swift 3和Alamofire从JSON下载图像? [英] How to download image from JSON using the Swift 3 and Alamofire?

查看:157
本文介绍了如何使用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.com; 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.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,您就可以下载图像。如果不是预期的答案,请重新编辑您的问题。

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 = "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";

这是一个有效的网址或路径,如果它是一个网址,你需要添加你的基本网址+您的/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

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

希望它有所帮助。

这篇关于如何使用Swift 3和Alamofire从JSON下载图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆