mchoe / SwiftSVG

A simple, performant, and lightweight SVG parser
Other
1.94k stars 229 forks source link

Unable to load local cached SVG image in UIView. Don't throw any error related to file location #59

Open nikhil-heady opened 6 years ago

nikhil-heady commented 6 years ago

We are caching the SVG files from server and trying to load the same via method UIView(SVGURL: svgURL) but it is not visible into view and also tried the sample URL and it loads. So does it need any specific format for loading image from local document directory ?

Sample Cached URL that i want to load into UIView.

file:///Users/nikhiltrivedi/Library/Developer/CoreSimulator/Devices/7B1ABC23-1B25-46EE-8520-F0AE12D2F84B/data/Containers/Data/Application/DA511A10-3772-4265-B36F-A647C7A821D0/Library/Caches/KeyDocuments/4607512156413564370.svg

mchoe commented 6 years ago

Hi @nikhilrtrivedi, You should be able to specify any local URL as that UIView initializer is a convenience initializer over the ability to load from Data. In other words, you should not have to do any special sort of formatting for the URL. It should load either local or remote URLs.

Just to check, are you ensuring that the file exists at that path? Without further information, it's hard to diagnose the problem and maybe you can provide some sample code.

nikhil-heady commented 6 years ago

@mchoe Hi, Thank you for quick reply. If file path is wrong i get the error message that file do not exists at path. So path is correct, still it is not loading the svg image.

nikhil-heady commented 6 years ago

@mchoe Hi, We are try to load svg into cell view. Code is as below.

import QuickLook
import PromiseKit
import Shimmer
import UIKit
import WebKit
import SwiftSVG

class KeyDocumentVC: UIViewController, UITableViewDelegate, UITableViewDataSource  {

    var name: String! = ""
    var pages: [GetFilesQuery.Data.FilesMessage.Page?] = []
    var shimmeringView: FBShimmeringView = FBShimmeringView()
    private var promises: [PromiseKit.Promise<URL>] = []
    //    private var promise: PromiseKit.Promise<URL>

    @IBOutlet weak var menuTableView: UITableView!
    @IBOutlet weak var largeTableView: UITableView!
    @IBOutlet weak var navigationBar: UINavigationBar!

    override func viewDidLoad() {
        super.viewDidLoad()

        menuTableView.dataSource = self
        menuTableView.delegate = self

        largeTableView.dataSource = self
        largeTableView.delegate = self
    }

    @objc func dismissMe() {
        dismiss(animated: true)
    }

    struct CellItem {
        let render: (CGSize) -> UIView
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)

        self.navigationBar.shadowImage = UIImage()
        self.navigationBar.setBackgroundImage(UIImage(), for: .default)

        for page in pages {
            let promise:PromiseKit.Promise<URL> = API.file(id: (page?.id)!)
            print("promise url ",promise)
            promises.append(promise)
        }

        when(fulfilled: promises ).then {_ in
            self.refreshData()
        }
    }

    @IBAction func returnHome(_ sender: Any) {

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    internal func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        //return 1
        return promises.count
    }

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

        var cell = tableView.dequeueReusableCell(withIdentifier: "menuCell", for: indexPath) as? KeyDocumentTableViewCell
        if cell == nil {
            cell = KeyDocumentTableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "menuCell")
        }

        cell?.pageIndexLabel.text = "\(indexPath.row)"
        let svgRect = (tableView == menuTableView) ? CGRect(x: 32, y: 48, width: 257, height: 292) : CGRect(x: 97, y: 43, width: 510, height: 575)

        //let dst = FileManager.cache/"KeyDocuments"/"\(rq.url!.hashValue).svg"

        if promises[indexPath.row].isResolved {
            let pageUrl = promises[indexPath.row].value!
            let fileName = pageUrl.lastPathComponent
            let dst = FileManager.cache/"KeyDocuments"/"\(fileName)"
            if FileManager.default.fileExists(atPath: dst.path) {
                //print("FILE Yes AVAILABLE")
            } else {
                //print("FILE NOT AVAILABLE")
            }

            ///let fileData = NSData(contentsOfFile: NSString(dst.path))
                let thisItem = CellItem(render: { (cellSize) -> UIView in
                    print(dst,"\n") //file:///Users/nikhiltrivedi/Library/Developer/CoreSimulator/Devices/140F07EB-3AE0-46A6-A318-F2A01367B859/data/Containers/Data/Application/EC3A53EB-BD75-4C23-88A1-3D203C27AEC9/Library/Caches/KeyDocuments/4607512156414687203.svg 
                    let hammock = UIView(SVGURL: dst) { (svgLayer) in
                        svgLayer.fillColor = UIColor.red.cgColor
                        svgLayer.resizeToFit(CGRect(x: 0, y: 0, width: cellSize.width, height: cellSize.height))
                    }
                    return hammock
                })

           cell?.svgView.addSubview(thisItem.render(CGSize(width: svgRect.size.width, height: svgRect.size.width)))
           //cell?.svgView.backgroundColor = UIColor.red
        }

        return cell!
    }

    internal func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return tableView == menuTableView ? 400 : 700
    }

    private func refreshData() {
        self.menuTableView.reloadData()
        self.largeTableView.reloadData()
    }

}
nikhil-heady commented 6 years ago

@mchoe Hi can you please check above updated code. What can be the issue. I have cross checked the file path it is valid one. I will appreciate if you can look into it.