1

i'm trying to load an image placed in InfoWindow via SDWebImage.

On first opening of this InfoWindow mainLogoImageView placing only placeholder. On second - loaded image. The problem is that image can't load on first opening of InfoWindow(it's not depends on how much time i'm waiting for loading image)

func mapView(mapView: GMSMapView!, markerInfoWindow marker: GMSMarker!) -> UIView! { 

var infoWindow : CustomInfoWindow = NSBundle.mainBundle().loadNibNamed("CustomInfoWindow", owner: self, options: nil)[0] as CustomInfoWindow

infoWindow.mainLogoImageView.sd_setImageWithURL(NSURL(string: self.sharedManager.myModel[0].img_logo), placeholderImage: UIImage(named: "logo_placeholder.png"))

return infoWindow

}

Thanks for helping

  • Does it show the place holder image in the first load? Also you can try to add a completion block with a print/long statement inside, so you can check if the image loaded successfully in the first click. Also you can try the setImageView method from AFNetworking. – ztan Feb 09 '15 at 17:18
  • @ztan yeah, it shows placeholder image in first. I've tried to add a completion block, but this block's calling at second click as loaded image. – Dmitry Chebnev Feb 09 '15 at 17:58
  • @ztan And using AFNetworking gives me same result – Dmitry Chebnev Feb 09 '15 at 18:39
  • @ztan When I use AFNetworking, completion block calls in first click, but it can't even hide ImageView (imageView.hidden = true). I think, that image can be set only if it's already loaded. So in second click it shows only because of it's cached. – Dmitry Chebnev Feb 09 '15 at 18:58
  • I tried SDWebImage, it only shows the image in the second click unfortunately. So which AFNetworking method you use? – ztan Feb 09 '15 at 21:48
  • I also tried it in Android with the third party image library Picasso, it shows the network request image in the second click, doesnt show image in the first click. – ztan Feb 09 '15 at 22:43

2 Answers2

4

insert marker.tracksInfoWindowChanges = true; in mapView function

Answer to your question

func mapView(mapView: GMSMapView!, markerInfoWindow marker: GMSMarker!) UIView! {   
   marker.tracksInfoWindowChanges = true;
   var infoWindow : CustomInfoWindow = NSBundle.mainBundle().loadNibNamed("CustomInfoWindow", owner: self, options: nil)[0] as CustomInfoWindow
   infoWindow.mainLogoImageView.sd_setImageWithURL(NSURL(string: self.sharedManager.myModel[0].img_logo), placeholderImage: UIImage(named: "logo_placeholder.png"))

   return infoWindow
} 
Community
  • 1
  • 1
Igor Kasuan
  • 792
  • 2
  • 10
  • 25
1

I think I might found a workaround for your problem. I wrote my code in Objective-C, hope you can translate it to Swift.

If you want to get completion callback, then you can try to use the SDWebImageMannager's downloadImageWithURL method.

But in order to make the image to be shown in the first time, you need to have an instance variable too.

So first, you need to declare an index counter instance variable:

int counter = 0; probably var counter = 0; in Swift

Then in your delegate method, you can do the following:

- (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker{

    InfoWindow *view =  [[[NSBundle mainBundle] loadNibNamed:@"InfoWindow" owner:self options:nil] objectAtIndex:0];

    couter++;

   [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:@"https://myimage.png"] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        NSLog(@"in progress");
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        NSLog(@"complete");
        view.imageView.image = image;
        if (counter == 1) {
            self.mapView.selectedMarker = marker;
        }

    }];

    return view;
}
ztan
  • 6,861
  • 2
  • 24
  • 44