jessedc / JCTiledScrollView

A set of classes that wrap UIScrollView and CATiledLayer. It aims to simplify displaying large images and PDFs at multiple zoom scales.
MIT License
310 stars 47 forks source link

Asynchronous tiles support #11

Open jessedc opened 12 years ago

jessedc commented 12 years ago

Here's some notes on how to get it working...

https://github.com/route-me/route-me http://wiki.openstreetmap.org/wiki/OSM_in_MapKit https://github.com/mladjan/GoogleMapsOverlayiOS

codesourse commented 6 years ago

Using NSCondition may solve it you can download image from internet.

Joohansson commented 6 years ago

Alamofire seems to be a nice option. It has built in support for URLSession, cache system, placeholder image, purging, image filters, HTTP Basic Auth etc. https://github.com/Alamofire/AlamofireImage

I started to experiment with this and created a simple asynchronous function to fetch images (not yet using alamofire). Could someone please point me in the right direction why my fetched images does not update in the tile view? It's just black. I guess the tiles needs to refresh somehow after they have been downloaded?

func tiledScrollView(_ scrollView: JCTiledScrollView, imageForRow row: Int, column: Int, scale: Int) -> UIImage? {
        // Ideally we have @3/6/12/24 tiles, but if not we need to change the original image size. See skippingGirlImageSize
        var tileScale = scale
        if (scale % 3 == 0) {
            tileScale = (scale * 10) / 15
        }

        //return UIImage(named: "tiles/SkippingGirl_\(tileScale)x_\(row)_\(column).png")
        var image: UIImage? = nil
        if let url = URL(string: "https://tile.openstreetmap.org/\(tileScale+1)/\(row)/\(column).png") {
            getDataFromUrl(url: url) { data, response, error in
                guard let data = data, error == nil else { return }
                DispatchQueue.main.async() {
                    guard let fetched = UIImage(data: data) else {
                        print ("Failed to fetch image: \(url)")
                        return
                    }
                    image = fetched
                }
            }
        }
        return image
    }

    func getDataFromUrl(url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {
        URLSession.shared.dataTask(with: url) { data, response, error in
            completion(data, response, error)
            }.resume()
    }