Ios 如何使用Swift 3和Alamofire从JSON下载图像?
如何使用Alamofire和Swift 3从JSON下载图像?我正在查数据字典。请参阅以下JSON响应。我可以打印标签中的数据,但无法下载图像。这是我从API得到的响应 userJson userJson[“状态”:1,“学生”:{“入学日期”=“14/06/2017”; “入学编号”=13538;“班主任”=Caroline Forbes;dob=Ios 如何使用Swift 3和Alamofire从JSON下载图像?,ios,json,swift,image,alamofire,Ios,Json,Swift,Image,Alamofire,如何使用Alamofire和Swift 3从JSON下载图像?我正在查数据字典。请参阅以下JSON响应。我可以打印标签中的数据,但无法下载图像。这是我从API得到的响应 userJson userJson[“状态”:1,“学生”:{“入学日期”=“14/06/2017”; “入学编号”=13538;“班主任”=Caroline Forbes;dob= “04/05/2001”;email=“ranisagar。sivadas@gmail.com“父亲的名字”= “萨加尔·西瓦达斯”;性别=男性;
“04/05/2001”;email=“ranisagar。sivadas@gmail.com“父亲的名字”= “萨加尔·西瓦达斯”;性别=男性;形象=
“/system/images/86/j1f9DiJi_medium.jpg?1504593436”;“母亲姓名”= “RANI R S”name=“Abhijith Sagar”;电话=9066260799;宗教= 印度教;“学校电子邮件”=“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
}
}
func SetUpUIProfiledata(){
apimager.sharedInstance.getParentDataFromURL(){(userJson)->中的Void
让swiftyJsonVar=JSON(userJson)
打印(“userJson userJson userJson”,userJson)
打印(“快速JSONVAR”,快速JSONVAR)
让message=swiftyJsonVar[“message”].rawString()
让sudent=swiftyJsonVar[“学生”].rawString()!
让jsonData=sudent.data(使用:.utf8)!
让dictionary=try?JSONSerialization.jsonObject(带有:jsonData,选项:[])作为!dictionary
self.name\u lbl.text=字典?[“name”]作为?字符串
self.fatherName\u lbl.text=字典?[“father\u name”]作为?字符串
self.motherName\u lbl.text=字典?[“mother\u name”]作为?字符串
self.phone\u lbl.text=字典?[“phone”]作为?字符串
self.email\u lbl.text=字典?[“电子邮件”]作为?字符串
self.dob\u lbl.text=字典?[“dob”]作为?字符串
self.gender\u lbl.text=字典?[“gender”]作为?字符串
self.religation\u lbl.text=字典?[“religation”]作为?字符串
self.admissionDate\u lbl.text=字典?[“入院日期”]作为?字符串
self.admissionNum\u lbl.text=字典?[“接纳号”]as?字符串
self.schoolMail\u lbl.text=字典?[“school\u email”]作为?字符串
self.teacher\u lbl.text=字典?[“class\u teacher”]作为?字符串
}
}
看看下面的代码,您只需将链接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+您的“/system/images/86/j1f9DiJi_medium.jpg”并将其传递给alamofire块以下载图像,那么这是一个有效的url或路径吗
就像我上面的例子一样
"https://www.planwallpaper.com" - baseURl
"static/images/9-credit-1.jpg" - image Path as in your case
希望有帮助。看看下面的代码,您只需将链接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+您的“/system/images/86/j1f9DiJi_medium.jpg”并将其传递给alamofire块以下载图像,那么这是一个有效的url或路径吗
就像我上面的例子一样
"https://www.planwallpaper.com" - baseURl
"static/images/9-credit-1.jpg" - image Path as in your case
希望能有所帮助。首先获取图像键对应的值,并将其与基本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
}
}
}
希望能有所帮助。首先获取图像键对应的值,并将其与基本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
}
}
}
希望能有所帮助。请将您的代码设置为有效格式。您尝试了哪些方法从您的回复中获取图像?同时也要检查图像密钥,无论您在浏览器中获得的路径是否正常工作?工作正常。@Nishant Bhindi请将您的代码设置为有效格式您尝试过从您的回复中获取图像吗?同时也要检查图像密钥,无论您在浏览器中获得的路径是否正常工作?工作正常。@Nishant Bhindi有图像url,如何将该图像传递到url基url您有基url,您的图像url是什么?只需将它们附加到一个字符串中,并在block中传递给finalUrl=Str1+str2或FinalStr=Str1和FinalStr.append(str2)并使用它,我的疑问是我从基本url获取图像,所以如何获取图像url以及如何附加到图像url。让FinalStr=baseUrl和FinalStr.append(imageUrlString)然后让strURL1:String=FinalStr,它被用于alamofirelet strURL1=baseUrl+imageUrl,并被用于alamofirei的图像url,以及如何将该图像传递到url base url你有基本url,你的图像url是什么?只需将它们附加到一个字符串中,并在block中传递给finalUrl=Str1+str2或FinalStr=Str1和FinalStr.append(str2)并使用它,我的疑问是我从基本url获取图像,所以如何获取图像url以及如何附加到图像url。让FinalStr=baseUrl和FinalStr.append(imageUrlString)然后让strURL1:String=FinalStr,它被用于alamofirelet strURL1=baseUrl+imageUrl,并被用于alamofire