【问题标题】:How to fetch image and pdf file from json and display in tableviewcell swift 3如何从json中获取图像和pdf文件并在tableviewcell swift 3中显示
【发布时间】:2018-05-17 16:21:02
【问题描述】:

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

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

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

以下是我尝试过的代码:

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

}    

【问题讨论】:

  • 一个 UIWebView 可以显示 pdf。并且文件名需要一个完整的 url,例如“http://www.../file.png”
  • @deltami:在这里给我看看你的 tableview dataSource 方法
  • @Bucket Question 用数据源方法更新。
  • let downimage = dowdict?.value(forKey: "file_name") 将只获得 downimage 变量中的字符串,即; “9d3ff19bd54e48a087947227ee36c68c_13417773.png”不是图片。
  • @deltami 图片的完整 URL 字符串是什么?您从哪个 url 获得这些 json 响应。

标签: json uitableview swift3 uiimageview


【解决方案1】:
    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})

在您的情况下,使用以下代码获取您的完整网址

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

    prefixUrl.append(imageUrlStr);

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

FOR PDF -- 因为 pdf 不显示在 image view 中,所以需要在 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);

【讨论】:

  • 嗨@Warewolf 现在仍然出现错误,它显示的索引超出范围。还有一件事,因为您编码 prefixurl.append 但在我的情况下我无法找到该方法,它显示了三种方法 1 )appendingPathComponent(pathComponent: String) 2)appendingPathExtension(pathExtension: String) 3) appendingPathComponent(pathComponent: String, isDirectory: Bool) 在这 3 个中我使用了 pathComponent:String 但没有得到想要的输出。
  • 再次嗨 @Warewolf m 仍在等待您的帮助。
  • 什么是您的基本网址(prefixUrl),您只需在图片的基本网址后添加您的文件名即可获得完整的图片网址,如dncache-mauganscorp.netdna-ssl.com/thumbseg/378/…
  • 使用这个 var prefixUrl:String = "apidomain.com";启用附加方法。您正在使用 NSString 而不是 String。或使用 prefixUrl.appendingFormat("%@", imageUrlStr);
  • 嗨 @warewolf 49.207.183.152:88/api/Student/… 检查此 APIURL 一旦你检查请 ping 我回来,所以我可以从这里删除。
【解决方案2】:

在行的单元格上尝试以下代码

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

cell2.dataimage.image = image

【讨论】:

  • 给我错误:无法将类型“UIImage”的值转换为预期的参数类型“数据”
  • 检查我更新的问题现在它在这一行崩溃:self.announimage.append(downimage as!UIImage)
  • let downimage = dowdict?.value(forKey: "file_name") 你在这里得到的只有 url 或 uiimage 吗?
  • var announimage : Array = [UIImage]() 它是不可变的,将 NSArray 更改为 NSMutableArray
  • 仍然因错误而崩溃:无法将“Swift._EmptyArrayStorage”(0x112bfc5a0)类型的值转换为“NSMutableArray”(0x1125ee548)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-11
  • 2017-12-02
  • 1970-01-01
  • 2012-09-24
相关资源
最近更新 更多