首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在快速4.1中从服务器获取图像

如何在快速4.1中从服务器获取图像
EN

Stack Overflow用户
提问于 2018-05-12 08:42:02
回答 1查看 2.3K关注 0票数 1

我在从服务器获取映像时遇到了问题,下面是我的代码导入UIKit

代码语言:javascript
运行
复制
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链接执行它时,我的邮递员就是这样的。

代码语言:javascript
运行
复制
<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"}

我的新代码和桌面视图:

代码语言:javascript
运行
复制
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是什么样子的:

代码语言:javascript
运行
复制
{
"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"
    }
]
}

我的模型创建代码:

代码语言:javascript
运行
复制
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
}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-12 09:19:23

你实际上点击了一个.php file.To get图像,你必须点击一个像.jpg or .png extension这样的图像文件,你点击的url也是HTML数据的响应,但是它应该用json or xml.Keep响应,看看rest,这有助于你理解更多。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50304379

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档