ios - 如何在 swift 4.1 中从服务器获取图像
问题描述
我在从服务器获取图像时遇到问题,这是我的代码导入 UIKit
class ViewController: UIViewController {
@IBOutlet weak var getImageOutlet: UIImageView!
let get_Image_URL = "http://172.16.1.22/UploadImage/displayImage.php"
override func viewDidLoad() {
super.viewDidLoad()
self.getImage()
// Do any additional setup after loading the view, typically from a nib.
}
func getImage(){
let session = URLSession(configuration: URLSessionConfiguration.default)
guard let url = URL(string: "http://172.16.1.22/UploadImage/displayImage.php") else { return }
var request = URLRequest(url: url)
request.httpMethod = "GET"
session.dataTask(with: request) { (data, response, error) in
if let error = error {
print("Something went wrong: \(error)")
}
if let imageData = data {
DispatchQueue.main.async {
self.getImageOutlet.image = UIImage(data: imageData)
}
}
}.resume()
}
}
如何使用任何其他方法从服务器获取图像我的图像未显示在我的 imageView 中,谁能帮我解决这个问题?
这就是我使用 api 链接执行邮递员时的样子
<img src='http://localhost/Book-My-BIM-API/Company-Registration-
API/v1/companyImages/harshilpatel@gmail.com_company-registration.jpg'
style='height:40%;width:40%;'/>
<img src='http://localhost/Book-My-BIM-API/Company-Registration-
API/v1/companyImages/shriyakshah@gmail.com_company-registration.jpg'
style='height:40%;width:40%;'/>
<img src='http://localhost/Book-My-BIM-API/Company-Registration-
API/v1/companyImages/johndoe@gmail.com_Image_Test.jpg'
style='height:40%;width:40%;'/>
<img src='http://localhost/Book-My-BIM-API/Company-Registration-
API/v1/companyImages/zara.larsson@gmail.com_Image_Test.jpg'
style='height:40%;width:40%;'/>
<img src='http://localhost/Book-My-BIM-API/Company-Registration-
API/v1/companyImages/_user-profile.jpg'
style='height:40%;width:40%;'/>
<img src='http://localhost/Book-My-BIM-API/Company-Registration-
API/v1/companyImages/ijk@gmail.com_user-profile.jpg'
style='height:40%;width:40%;'/>
<img src='http://localhost/Book-My-BIM-API/Company-Registration-
API/v1/companyImages/def@gmail.com_user-profile.jpg'
style='height:40%;width:40%;'/>
<img src='http://localhost/Book-My-BIM-API/Company-Registration-
API/v1/companyImages/mno@email.com_user-profile.jpg'
style='height:40%;width:40%;'/>
<img src='http://localhost/Book-My-BIM-API/Company-Registration-
API/v1/companyImages/Dhdnm_user-profile.jpg'
style='height:40%;width:40%;'/>
<img src='http://localhost/Book-My-BIM-API/Company-Registration-
API/v1/companyImages/abc@email.com_user-profile.jpg'
style='height:40%;width:40%;'/>{"message":"Image displayed
Successfully"}
我的新代码与 tableview :
import UIKit
class ImageTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
final let myURL = URL(string: "http://172.16.1.22/UploadImage/displayImage.php")
var imageArray = [ImageData]()
@IBOutlet weak var imageTableVieqw: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.imageTableVieqw.delegate = self
self.imageTableVieqw.dataSource = self
self.downloadImage()
// self.imageTableVieqw.tableFooterView = UIView()
// Do any additional setup after loading the view.
}
func downloadImage(){
guard let downloadURL = myURL else{ return }
URLSession.shared.dataTask(with: downloadURL) { (data1, response, error) in
print("Downloaded")
guard let data = data1, error == nil, response != nil else{
print("Something went wrong")
return
}
do {
let decoder = JSONDecoder()
let downloadedimagedata = try decoder.decode(Images.self, from: data)
print("ImageDATA=\(downloadedimagedata)")
// print("image***=\(downloadedimagedata.images[1].imageV)")
self.imageArray = downloadedimagedata.images
DispatchQueue.main.async {
self.imageTableVieqw.reloadData()
}
}
catch{
print("Catch wrong")
}
}.resume()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return imageArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "imageCell") as! ImageTableViewCell
if let imageURL = URL(string: imageArray[indexPath.row].company_logo)
{
DispatchQueue.global().async {
let data = try? Data(contentsOf: imageURL)
if let data = data{
let image = UIImage(data: data)
DispatchQueue.main.async {
cell.imageCell.image = image
}
}
}
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我的新 API 长什么样:
{
"logos": [
{
"company_logo": "http://172.16.1.22/Book-My-BIM-API/Company-Registration-API/v1/companyImages/harshilpatel@gmail.com_company-registration.jpg"
},
{
"company_logo": "http://172.16.1.22/Book-My-BIM-API/Company-Registration-API/v1/companyImages/shriyakshah@gmail.com_company-registration.jpg"
},
{
"company_logo": "http://172.16.1.22/Book-My-BIM-API/Company-Registration-API/v1/companyImages/johndoe@gmail.com_Image_Test.jpg"
},
{
"company_logo": "http://172.16.1.22/Book-My-BIM-API/Company-Registration-API/v1/companyImages/zara.larsson@gmail.com_Image_Test.jpg"
},
{
"company_logo": "http://172.16.1.22/Book-My-BIM-API/Company-Registration-API/v1/companyImages/_user-profile.jpg"
},
{
"company_logo": "http://172.16.1.22/Book-My-BIM-API/Company-Registration-API/v1/companyImages/ijk@gmail.com_user-profile.jpg"
},
{
"company_logo": "http://172.16.1.22/Book-My-BIM-API/Company-Registration-API/v1/companyImages/def@gmail.com_user-profile.jpg"
},
{
"company_logo": "http://172.16.1.22/Book-My-BIM-API/Company-Registration-API/v1/companyImages/mno@email.com_user-profile.jpg"
},
{
"company_logo": "http://172.16.1.22/Book-My-BIM-API/Company-Registration-API/v1/companyImages/Dhdnm_user-profile.jpg"
},
{
"company_logo": "http://172.16.1.22/Book-My-BIM-API/Company-Registration-API/v1/companyImages/abc@email.com_user-profile.jpg"
},
{
"company_logo": "http://172.16.1.22/Book-My-BIM-API/Company-Registration-API/v1/companyImages/erp@email.com_user-profile.jpg"
},
{
"company_logo": "http://172.16.1.22/Book-My-BIM-API/Company-Registration-API/v1/companyImages/tuv@email.com_user-profile.jpg"
},
{
"company_logo": "http://172.16.1.22/Book-My-BIM-API/Company-Registration-API/v1/companyImages/rohansoni@gmail.com_Image_Test.jpg"
},
{
"company_logo": "http://172.16.1.22/Book-My-BIM-API/Company-Registration-API/v1/companyImages/qwe@email.com_user-profile.jpg"
}
]
}
我的模型创建代码:
import Foundation
class Images: Codable {
let images: [ImageData]
init(images: [ImageData]) {
self.images = images
}
}
class ImageData: Codable {
let company_logo: String
init(company_logo: String) {
self.company_logo = company_logo
}
}
解决方案
您实际上点击了一个.php
文件。要获取图像,您必须点击一个图像文件.jpg or .png extension
,并且url
您点击的是HTML
数据响应,但它应该响应json or xml
。继续查看 rest api。它可以帮助您了解更多。
推荐阅读
- python - 如何从csv文件中选择3列系数的数据
- php - php xpath替换不具有一定的价值
- python - Django - 初始值模型
- spring-boot - Spring Boot Actuator - 多个健康端点
- javascript - 为什么“in”运算符会使用字符串文字而不是记录错误来引发错误?
- javascript - 如何使用 javascript 代码进行 pagespeed 洞察 api 显示我的网站的结果
- python - 如何使用新打开的文件恢复解析
- android - RxJava (+Room):为什么是 Flowable
- > 返回列表
- >?
- java - Maven 和 java:如何从测试目录中的 protobuf 文件生成代码?
- java - 如何访问 Jar 文件中的文件