使用Google Maps SDK for iOS优化自定义标记图片的性能 [英] Optimising custom marker image performance with Google Maps SDK for iOS

查看:101
本文介绍了使用Google Maps SDK for iOS优化自定义标记图片的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在iOS应用中加入了Google Maps for iOS SDK。这个应用程序旨在检索飞机的位置(包括飞机模型,经纬度,速度 - 飞机的基本价值),然后将它们绘制在Google Map上。

现在,最近,API(我正在使用的那个)返回的飞机数量翻了一番,几乎翻了三倍。每次我尝试运行应用程序时,我都没有问题,它崩溃了,并给我出现以下错误:

 (( null))为false:达到纹理图集的最大数量,无法分配更多。 

我在SDK的Google Code上发现了这个问题页面:https://code.google.com/p/gmaps-api-issues/issues/detail?id=5756 - 在这里,我相信我的崩溃问题是由我使用的自定义标记图像的数量决定的。每个飞机模型都有不同的图像,这些图像在渲染时被加载,UIImage被分配到GMSMarker。



现在,我遇到的问题是大量的结果,我得到这个崩溃。同时,我也想为每个标记提供单独的图像。



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



感谢您的帮助,请让我知道如果我没有让自己清楚!

解决方案



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


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


我在遍历对象列表来创建每个标记: / p>

  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 =图片;
marker.map = mapView_;
}

所以在这里,我将图像复制到每个标记。这占用了不必要的资源。我的解决方案:

  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 =图片;
marker.map = mapView_;



$ b

在for循环外定义UIImage实例意味着图像被引用来自每个标记,不针对每个标记重新呈现。在此之后,内存使用率要低得多。

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, speed - basic values for an aircraft) and then plot them on a Google Map.

Now, recently, the number of aircraft that the API (the one I am using) is returning has doubled, nearly tripled. I had no issues before yet each time I attempt to run the app, it crashes, giving me the following error:

((null)) was false: Reached the max number of texture atlases, can not allocate more.

I found this issue page on Google Code for the SDK: https://code.google.com/p/gmaps-api-issues/issues/detail?id=5756 - here, I am made to believe that the issue with my crash is down to the number of custom marker images I am using. Each aircraft model has a different image, and these are loaded as they are rendered and a UIImage assigned to a GMSMarker.

Now, the problem I have is with a large amount of results, I get this crash. At the same time, I also want to have individual images for each marker.

My question is, is there a way that instead of assigning each marker a UIImage of a specific aircraft, can I refer once to each image to optimise performance?

I appreciate your help, please let me know if I haven't made myself clear!

解决方案

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.

这篇关于使用Google Maps SDK for iOS优化自定义标记图片的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆