使用Google Maps SDK for iOS优化自定义标记图像性能

Cam*_*nso 16 google-maps objective-c ios google-maps-sdk-ios

我最近在iOS应用中整合了Google Maps for iOS SDK.此应用程序旨在检索飞机的位置(包括飞机模型,纬度/经度,速度 - 飞机的基本值),然后在谷歌地图上绘制它们.

现在,最近,API(我正在使用的)返回的飞机数量增加了一倍,几乎增加了两倍.之前我没有遇到任何问题我每次尝试运行应用程序时都会崩溃,并给出以下错误:

((null)) was false: Reached the max number of texture atlases, can not allocate more.
Run Code Online (Sandbox Code Playgroud)

我在SDK的Google代码上找到了此问题页面:https://code.google.com/p/gmaps-api-issues/issues/detail?id = 5756 - 在这里,我被认为是与我的问题崩溃是我正在使用的自定义标记图像的数量.每个飞机模型都有不同的图像,这些图像在渲染时加载,UIImage分配给GMSMarker.

现在,我遇到的问题是有大量的结果,我得到了这个崩溃.同时,我还希望为每个标记提供单独的图像.

我的问题是,有没有一种方法,而不是为每个标记分配特定飞机的UIImage,我可以一次参考每个图像来优化性能吗?

感谢您的帮助,如果我没有说清楚,请告诉我!

Cam*_*nso 22

在再次遇到这个问题之后回答我自己的问题.

问题似乎是我为每个标记分配一个单独的UIImage实例.这意味着当我在GMSMapView实例上绘制标记时,每个标记都有一个单独的UIImage.这里简要介绍一下:自定义标记图像 - 适用于iOS的Google Maps SDK.

如果要使用相同的图像创建多个标记,请为每个标记使用相同的UIImage实例.这有助于在显示许多标记时提高应用程序的性能.

我正在迭代一个对象列表来创建每个标记:

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_;
}
Run Code Online (Sandbox Code Playgroud)

所以在这里,我将图像复制到每个标记.这需要更多的资源.我的解决方案:

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_;
}
Run Code Online (Sandbox Code Playgroud)

在for-loop之外定义UIImage实例意味着图像是从每个标记引用的,而不是为每个标记重新呈现.此后内存使用率要低得多.