How to get image and pdf file from json and display in tableviewcell swift 3

Hello everyone, because I am Fresher, so I don’t know how to get the image and PDF file from the API and how to display it in tableviewcell so that the user can see the image and PDF file, and also load it from there. I tried the code for but it does not work and does not have a single idea for pdf. Please, help.

code for image: I declared an array of images as shown below:

var announimage : Array = [UIImage]()

Below is the format of my JSON data.

{
 "Download_Details": [
 {
"school_id": 1,
"class_id": "Pre - KG ",
"section": "A ",
"file_name": "427a3be0155b49db999cffb51f078f9c_431875732.pdf",
"title": "TeQ",
"message": "Hello Users",
"date": null
},
{
"school_id": 1,
"class_id": "Pre - KG ",
"section": "A ",
"file_name": "dce5ab53f45f4ec5b52894bc6286ed35_1376021.jpg",
"title": "Welcome",
"message": "Welcomes you.",
"date": null
},
{
 "school_id": 1,
 "class_id": "Pre - KG ",
 "section": "A ",
 "file_name": "9d3ff19bd54e48a087947227ee36c68c_13417773.png",
 "title": "TEST",
"message": "TEST",
"date": null
}
],
}

Below is the code I tried:

let responseString = try! JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary

let downarray = responseString?.value(forKey: "Download_Details") as? NSArray

for down in downarray!
{
    let dowdict = down as? NSDictionary

    let dmsgname = dowdict?.value(forKey: "message")

    self.announmessage.append(dmsgname as! String)

   let downimage = dowdict?.value(forKey: "file_name")

   self.announimage.append(downimage as! UIImage)


}

DispatchQueue.main.async                                    
{
    self.annountable.reloadData()
}

UITableView DataSource:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell2 = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! AnnounceCellTableViewCell
    cell2.announlabel.text = announmessage[indexPath.row]
    cell2.announdate.text = announcedates[indexPath.row]
    let image :UIImage = UIImage(data:announimage[indexPath.row] as! Data)!
    cell2.dataimage.image = image       
     return cell2

}    
+6
source share
2 answers

Try entering the code in the cell for the row

let image : UIImage = UIImage(data: filteredAry[indexPath.row] as! Data)

cell2.dataimage.image = image
+1
source
    let imgUrlStr:String = "https://dncache-mauganscorp.netdna-ssl.com/thumbseg/378/378006-bigthumbnail.jpg";

    if let imgURL = URL.init(string: imgUrlStr) {
        do {
            let imageData = try Data(contentsOf: imgURL as URL);
            let image = UIImage(data:imageData);
            print("here you will find your image:- \(String(describing: image)) ");
        } catch {
            print("Unable to load data: \(error)")
        }
    }

: -

here you will find your image:- Optional(<UIImage: 0x6080000bca40>, {450, 338})

URL-,

    var prefixUrl = "http://apidomain.com/";
    let imageUrlStr = dowdict?.value(forKey: "file_name")

    prefixUrl.append(imageUrlStr);

    print("full url \(prefixUrl)");

PDF - PDF , webView. webView pdf.

    let webView = UIWebView.init();
    let pdfUrlStr:String = "http://www.pdf995.com/samples/pdf.pdf";
    let pdfUrl = URL.init(string: pdfUrlStr);
    let urlRequest = URLRequest.init(url: pdfUrl!, cachePolicy: URLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 30.0);
    webView.loadRequest(urlRequest);
+1

All Articles