【问题标题】:How to download image from JSON using the Swift 3 and Alamofire?如何使用 Swift 3 和 Alamofire 从 JSON 下载图像?
【发布时间】:2017-09-12 04:36:23
【问题描述】:

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

userJson userJson userJson userJson ["status": 1, "student": { "admission_date" = "14/06/2017"; “admission_no” = 13538; "class_teacher" = "卡罗琳福布斯";出生日期 =
“2001 年 4 月 5 日”;电子邮件=“ranisagar.sivadas@gmail.com”; “父亲名字” = “萨加尔·西瓦达斯”;性别=男性;图片 =
"/system/images/86/j1f9DiJi_medium.jpg?1504593436"; “母亲姓名” = “拉尼RS”; name = "阿比吉特萨加尔";电话 = 9066260799;宗教= 印度教; "school_email" = "13538.861@demo.in"; “学生ID”= 86; },
“消息”:已成功获取详细信息。]

这是我的代码。

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

【问题讨论】:

  • 请输入有效格式的代码
  • 您尝试过什么从您的回复中获取图像?并且还检查图像密钥,无论您在浏览器中使用什么路径?
  • 它工作正常。 @Nishant Bhindi

标签: ios json swift image alamofire


【解决方案1】:

查看下面的代码,您只需将链接 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 + your " /system/images/86/j1f9DiJi_medium.jpg?1504593436" 并将它传递给 alamofire 块以下载图像 和我上面的例子一样

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

希望对你有帮助。

【讨论】:

  • 我有图片 url 以及如何将该图片传递给 url base url
  • 你有基本网址和你的图片网址是吗?只需将它们附加到一个字符串并在块中传递它 let finalUrl = Str1 + str2 或 FinalStr = str1 和 FinalStr.append(str2) 并使用它
  • 我怀疑我是从基本 url 获取图像,所以如何获取该图像 url 以及如何附加到图像 url。
  • 让FinalStr = baseUrl and FinalStr.append(imageUrlString) 然后让strURL1:String = FinalStr 用于alamofire
  • 让 strURL1 = baseUrl + imageUrl 用于 alamofire
【解决方案2】:

首先获取图像键对应的值并将其与您的基本 URL 匹配。喜欢

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

现在为它创建一个目标路径

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

现在使用类似Alamofire的下载方法

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

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2018-09-01
    • 1970-01-01
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-01
    • 2017-04-29
    相关资源
    最近更新 更多