简体   繁体   中英

How to show image from the imagePath in swift3

I have response from soap as image_path

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

This is my response

" http://sf.iipl.info/ImageCSharp.aspx?Location=C:\\\\infinityhost\\\\demo11.iipl.info\\\\data\\\\app\\\\user_location_photo\\\\8000034939_Resize\\\\&FileName=1_35f28e55-ca31-4918-8b41-9eb7f3070ace "

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


    }

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM