Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
418 views
in Technique[技术] by (71.8m points)

ios - How to download image from JSON using the Swift 3 and Alamofire?

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" = "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.]

This is my code.

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
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

    }
})

I looked at output you had provided you are getting

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

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

Hope it helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...