如何从json获取图像和pdf文件并在tableviewcell swift 3中显示

大家好,因为我是一个新手所以不知道如何从API获取图像和pdf文件以及如何在tableviewcell中显示,以便用户可以看到图像和pdf文件以及从那里下载它.我尝试了一些图像代码,但它不工作,没有一个单一的想法pdf.Please帮助.

图像代码
我已经声明了一系列图像,如下所示:

var announimage : Array = [UIImage]()

以下是我的JSON数据的格式.

{
 "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,"file_name": "dce5ab53f45f4ec5b52894bc6286ed35_1376021.jpg","title": "Welcome","message": "Welcomes you.",{
 "school_id": 1,"file_name": "9d3ff19bd54e48a087947227ee36c68c_13417773.png","title": "TEST","message": "TEST","date": null
}
],}

以下是我尝试过的代码

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数据源:

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

}

解决方法

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)");

FOR PDF – 由于pdf不在图像视图中显示,因此您需要在webView中显示它.只需在webView for 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);

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...