在uiwebview中呈现svg会导致内存压力(和应用程序崩溃)

时间:2022-11-20 17:50:51

I am working on an 'interactive map', similar to this example. I needed to be able to zoom in the map and still get a clean illustration, so the drawing of the map is done in svg (it actually was created in Illustrator, then saved as svg file).

我正在研究一个类似于这个例子的“交互式地图”。我需要能够放大地图并得到一个干净的插图,因此地图的绘制是在svg中完成的(它实际上是在Illustrator中创建的,然后保存为svg文件)。

In the app, I create a UIWebView and load the html containing the svg. The HTML file is a bundle resource in the app, so there's no network delay. There are tappable shapes that when you tap them, it brings you to another map (another html page with an svg drawing).

在这个应用程序中,我创建了一个UIWebView并加载包含svg的html。HTML文件是应用程序中的捆绑资源,因此不存在网络延迟。有一些可拖拽的形状,当您点击它们时,它会将您带到另一个映射(另一个带有svg绘图的html页面)。

One of the svg is rather large (~5.8MB). When loading, I get 'Memory Pressure' warnings then it crashes. Instruments shows Virtual Memory at 298.71MB and Real Memory at 149.10MB at time of crash.

其中一个svg相当大(约5.8MB)。加载时,我收到“内存压力”警告,然后它就崩溃了。仪器显示的虚拟内存为298.71MB,实际内存为149.10MB。

I already simplified the drawing (I took out some details), and I also used @Peter Collingridge svg optimizer.

我已经简化了绘图(我去掉了一些细节),我还使用了@Peter Collingridge svg优化器。

How can I optimize memory use so it won't crash?

如何优化内存使用,使其不会崩溃?

1 个解决方案

#1


1  

I resolved my issue:

我解决的问题:

  • I was creating the webview in my viewWillAppear:method, before calling [super viewWillAppear:animated]. I originally did this because I have a super class that instantiates common elements for all the different maps I have and that way the elements appeared in the correct order. The call to super is now done before anything else and I bring the elements to front.

    我在viewWillAppear:method中创建webview,然后调用[super viewWillAppear:animated]。我最初这么做是因为我有一个超类,它为我拥有的所有不同的映射实例化公共元素,这样元素以正确的顺序出现。对super的调用现在已经完成,我把元素放在前面。

  • I am now setting my webview to nil before creating and pushing another map (so another webview with other content).

    我现在正在将webview设置为nil,然后创建并推送另一个map(因此另一个webview带有其他内容)。

  • The first webview was created by the storyboard. It is now all done in code.

    第一个webview是由故事板创建的。现在都是在代码中完成的。

  • The drawings of the maps have been simplified, so the files are lighter in size. (Less details)

    地图的图纸已经简化了,所以文件比较轻。(细节)

Memory Report shows that Memory Utilized is now around 81.9 Mb (16.2% of available memory on an iPod Touch 5th generation).

内存报告显示,使用的内存现在约为81.9 Mb(相当于第5代iPod Touch可用内存的16.2%)。

I found that strange that I had a memory pressure warning with a (rather) small svg file. Also, the gage showed percentage never higher that 50%. I was not considering the JS engine and the webview memory limits. (Javascript engine is said to get full power in iOS 8, as reported here). (I couldn't find any official documentation about memory limits, probably because it seems to vary from device to device. See this answer.)

我发现奇怪的是,我有一个(相当)小的svg文件的内存压力警告。而且,保险标的百分比从未超过50%。我没有考虑JS引擎和webview内存限制。(据说Javascript引擎在ios8中获得了全力支持,如本文所述)。(我找不到任何有关内存限制的官方文档,可能是因为不同设备之间似乎存在差异。看到这个答案。)

In the end, loading the page is still long on my iPhone 4s or my iPod Touch 5th generation, but it works. If you want to suggest a better way to achieve this kind of map, you are welcome to do so.

最后,在我的iPhone 4s或我的iPod Touch第5代上加载页面仍然很长,但它确实有效。如果您想提出更好的实现这种地图的方法,欢迎您这样做。

#1


1  

I resolved my issue:

我解决的问题:

  • I was creating the webview in my viewWillAppear:method, before calling [super viewWillAppear:animated]. I originally did this because I have a super class that instantiates common elements for all the different maps I have and that way the elements appeared in the correct order. The call to super is now done before anything else and I bring the elements to front.

    我在viewWillAppear:method中创建webview,然后调用[super viewWillAppear:animated]。我最初这么做是因为我有一个超类,它为我拥有的所有不同的映射实例化公共元素,这样元素以正确的顺序出现。对super的调用现在已经完成,我把元素放在前面。

  • I am now setting my webview to nil before creating and pushing another map (so another webview with other content).

    我现在正在将webview设置为nil,然后创建并推送另一个map(因此另一个webview带有其他内容)。

  • The first webview was created by the storyboard. It is now all done in code.

    第一个webview是由故事板创建的。现在都是在代码中完成的。

  • The drawings of the maps have been simplified, so the files are lighter in size. (Less details)

    地图的图纸已经简化了,所以文件比较轻。(细节)

Memory Report shows that Memory Utilized is now around 81.9 Mb (16.2% of available memory on an iPod Touch 5th generation).

内存报告显示,使用的内存现在约为81.9 Mb(相当于第5代iPod Touch可用内存的16.2%)。

I found that strange that I had a memory pressure warning with a (rather) small svg file. Also, the gage showed percentage never higher that 50%. I was not considering the JS engine and the webview memory limits. (Javascript engine is said to get full power in iOS 8, as reported here). (I couldn't find any official documentation about memory limits, probably because it seems to vary from device to device. See this answer.)

我发现奇怪的是,我有一个(相当)小的svg文件的内存压力警告。而且,保险标的百分比从未超过50%。我没有考虑JS引擎和webview内存限制。(据说Javascript引擎在ios8中获得了全力支持,如本文所述)。(我找不到任何有关内存限制的官方文档,可能是因为不同设备之间似乎存在差异。看到这个答案。)

In the end, loading the page is still long on my iPhone 4s or my iPod Touch 5th generation, but it works. If you want to suggest a better way to achieve this kind of map, you are welcome to do so.

最后,在我的iPhone 4s或我的iPod Touch第5代上加载页面仍然很长,但它确实有效。如果您想提出更好的实现这种地图的方法,欢迎您这样做。