如何在swift3中显示来自imagePath的图像 [英] How to show image from the imagePath in swift3

查看:20
本文介绍了如何在swift3中显示来自imagePath的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到了soap作为image_path的回应

I have response from soap as image_path

(self.posts.value(forKey: "image_path") as! [String])[indexPath.row]

(self.posts.value(forKey: "image_path") as! [String])[indexPath.row]

这是我的回答

"http://sf.Sharpfination.asp?inC/Loop\\demo11.iipl.info\\data\\app\\user_location_photo\\8000034939_Resize\\&FileName=1_35f28e55-ca31-4918-8b41-9eb7f3070ace"

我无法将此响应转换为我的 url,我想在我的 tableViewCell 中显示此图像

i cant convert this response to my url, i want to show this image in my tableViewCell

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        print((self.posts.value(forKey: "image_path") as! [String])[indexPath.row])

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! AlllLocationTableViewCell

         let urlString = (self.posts.value(forKey: "image_path") as! [String])[indexPath.row] as String
        print("\(urlString)")



        let fileUrl = NSURL(fileURLWithPath: (self.posts.value(forKey: "image_path") as! [String])[indexPath.row])
        print(fileUrl as Any)



            let data = try? Data(contentsOf: fileUrl) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch

                self.Responseimage = UIImage(data: data!)!



        cell.LocationImage?.image = LocationImagView1
    return cell as UITableViewCell


    }

推荐答案

首先将 SDWebImage 库导入到您的项目中,然后根据您的要求在您的视图控制器中编写以下代码.

First of all import SDWebImage library into your project then write the following code in your view controller as per your requirements.

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ItemCell", for: indexPath as IndexPath) as! ItemCell

    let urlStr = "https:\\images.com\image\12345.png".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)

    print("URL :: \(urlStr!)");

    cell.imgItem.sd_setImage(with: URL(string: urlStr!), placeholderImage: UIImage(named: "item_placeholder"), options: [.refreshCached, .retryFailed, .allowInvalidSSLCertificates])

    }else{
        cell.imgItem.image = UIImage (named: "item_placeholder")
    }

    return cell
}

这篇关于如何在swift3中显示来自imagePath的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆