1

I am having problem in fetching image from the server, here is my code import 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()
}
}

how to fetch image from server with any other method my image is not displaying in my imageView can anyone help me with the problem ??

This is how my postman looks like when i execute it with the api link

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

My new Code with 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.
}

}

How my new API looks like:

{
"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"
    }
]
}

My model creation code:

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
}
}
Audrey Rozario
  • 59
  • 1
  • 10

1 Answers1

0

you actually hit a .php file.To get image you have to hit an image file like .jpg or .png extension and also the url you hit is response with HTML data but it should response with json or xml.Keep a look on rest api.It's helps you to understand more.

MD. Rejaul Hasan
  • 168
  • 1
  • 15
  • i would like to know that if i want a list of images from my server then also i need to hit a file like .jpg or .png – Audrey Rozario May 12 '18 at 09:34
  • look, you want a list of image.In your server site each image may store in a folder.So each image file has a path by which we can reach this image.So you need a list of path to get those image.And the way we get is a json response. – MD. Rejaul Hasan May 12 '18 at 09:43
  • ok I have made my backend developer to change the api format as you said now i want to return image in tableview can you let me know how to do that – Audrey Rozario May 14 '18 at 05:24
  • hit the link as you coded above.The code you do has no problem.The problem you have is the link.Hope it helps and accept any answer as correct if solve your problem. – MD. Rejaul Hasan May 14 '18 at 07:41
  • my code is going in catch I changed my code because i wanted the images to come it tableview but it's going in catch – Audrey Rozario May 14 '18 at 10:25
  • { "logos": [ { "company_logo": "http://172.16.1.22/Book-My-BIM-API/Company-Registration-API/v1/companyImages/qwe@email.com_user-profile.jpg" } ] } This is how my api looks now i have a list of images like this – Audrey Rozario May 14 '18 at 10:27
  • so what's problem.parse json and hit the image link.and when you download an image from web make sure it's a background task not in your main thread.Read GCD it may help you about threading. – MD. Rejaul Hasan May 14 '18 at 11:07
  • my code is going in catch and not displaying images and yes i will read about GCD – Audrey Rozario May 14 '18 at 11:25
  • Please study some basics of how network calling work.How you store data in your model.there many tutorial in google.I just give a [likn](https://classroom.udacity.com/courses/ud421) if you want to learn. – MD. Rejaul Hasan May 14 '18 at 12:52
  • Thank you so much for your help till now but can you let me know why is it going in catch I have updated my code with my model creation in it too – Audrey Rozario May 15 '18 at 03:48