Optimising custom marker image performance with Google Maps SDK for iOS

前端 未结 3 876
深忆病人
深忆病人 2021-01-04 02:43

I have recently been incorporating the Google Maps for iOS SDK within an iOS app. This app is designed to retrieve position of aircraft (including aircraft model, lat/lng, s

3条回答
  •  清酒与你
    2021-01-04 03:18

    Answering my own question after coming across the issue again.

    The problem appears to be that I am allocating a separate UIImage instance to each marker. This means that when I am plotting markers on the GMSMapView instance, each marker has a separate UIImage. This is described briefly here: Customize the marker image - Google Maps SDK for iOS.

    If you are creating several markers with the same image, use the same instance of UIImage for each of the markers. This helps improve the performance of your application when displaying many markers.

    I was iterating through a list of objects to create each marker:

    for (int i = 0; i < [array count]; i++) {
        UIImage *image = [UIImage imageWithContentsOfFile:@"image.png"];
        CLLocationCoordinate2D position = CLLocationCoordinate2DMake(10, 10);
        GMSMarker *marker = [GMSMarker markerWithPosition:position];
        marker.title = @"Hello World";
        marker.icon = image;
        marker.map = mapView_;
    }
    

    So here, I was copying the image to each marker. That took up more resources than necessary. The solution for me:

    UIImage *image = [UIImage imageWithContentsOfFile:@"image.png"];
    
    for (int i = 0; i < [array count]; i++) {
        CLLocationCoordinate2D position = CLLocationCoordinate2DMake(10, 10);
        GMSMarker *marker = [GMSMarker markerWithPosition:position];
        marker.title = @"Hello World";
        marker.icon = image;
        marker.map = mapView_;
    }
    

    Defining the UIImage instance outside of the for-loop meant that the image was referenced from each marker, not re-rendered for each marker. Memory usage was much lower after this.

提交回复
热议问题