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

浪子不回头ぞ 提交于 2019-12-25 09:08:03

问题


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
    }
}

回答1:


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.




回答2:


First get the image key corresponding value & marge it with your base URL. Like

let url = "www.yourbaseurlhere.com" + "/system/images/86/j1f9DiJi_medium.jpg?1504593436"

Now create a destination path for it like

let destination = DownloadRequest.suggestedDownloadDestination(
     for: .documentDirectory,
     in: .userDomainMask
 )

Now use Alamofire download method like

Alamofire.download(url, to: destination)
     .downloadProgress { progress in
            print("Download Progress: \(progress.fractionCompleted)")
         }
         .responseData { response in
             if response.result.value != nil {
                   if let directoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as? URL {

                        let fileName = response.response?.suggestedFilename // get your file name here.
                        let finalPath = directoryURL.appendingPathComponent(fileName!) //get your file path here
                    }
              }
         }

Hope it helps.



来源:https://stackoverflow.com/questions/46168008/how-to-download-image-from-json-using-the-swift-3-and-alamofire

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!