JCTiledScrollView icon indicating copy to clipboard operation
JCTiledScrollView copied to clipboard

Asynchronous tiles support

Open jessedc opened this issue 11 years ago • 2 comments

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

jessedc avatar Sep 14 '12 04:09 jessedc

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

  • (void)download { [self.condition lock]; //TODO: download code if (donloadFinish) { // after download send signal
    [self.condition signal]; [self.condition unlock]; } }
  • (void)doStuffWithDownloadPicture { [self.condition lock]; while (!donloadFinish) { [self.condition wait]; } //TODO: get image [self.condition unlock]; }

codesourse avatar Apr 26 '18 10:04 codesourse

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()
    }

Joohansson avatar Jun 25 '18 11:06 Joohansson