Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 手表套件应用程序:由于内存错误而终止_Ios_Swift_Watchkit - Fatal编程技术网

Ios 手表套件应用程序:由于内存错误而终止

Ios 手表套件应用程序:由于内存错误而终止,ios,swift,watchkit,Ios,Swift,Watchkit,您好,我正在开发一个应用程序,其中我需要更改50个图像(所有图像的大小为2.5 mb),它正在更改图像,但由于应用程序崩溃,Apple Watch应用程序的内存也增加了10 mb Xcode在Xcode“来自调试器的消息:由于内存错误而终止”中出错 我使用的代码如下: for (var i : Int = 1; i<26; i++) { let filenameHuman = NSString(format: "human_%d", i )

您好,我正在开发一个应用程序,其中我需要更改50个图像(所有图像的大小为2.5 mb),它正在更改图像,但由于应用程序崩溃,Apple Watch应用程序的内存也增加了10 mb

Xcode在Xcode“来自调试器的消息:由于内存错误而终止”中出错

我使用的代码如下:

 for (var i : Int  = 1; i<26; i++) {

            let filenameHuman = NSString(format: "human_%d", i )
            let filenameZombie = NSString(format: "zombie_%d", i )

            var imageHuman : UIImage! =  UIImage(named: filenameHuman as String)
            var imageZombie : UIImage! =  UIImage(named: filenameZombie as String)

            WKInterfaceDevice.currentDevice().addCachedImage(imageZombie, name: filenameZombie as String)

            WKInterfaceDevice.currentDevice().addCachedImage(imageHuman, name: filenameHuman as String)

        }

        NSLog("Currently cached images: %@",WKInterfaceDevice.currentDevice().cachedImages)

for(var i:Int=1;i在循环中尝试自动释放图像使用的内存,因为您不想等待稍后方法返回时自动释放

for (var i : Int  = 1; i<26; i++) {
    autoreleasepool {
      /* code to cache images */ 
    }
}
for(变量i:Int=1;i
  • 您的图像中是否有动画(会占用更多空间)
  • 收集对addCachedImage()的每次调用的返回值。False表示无法添加它——您需要对此进行检查,它可能会提供有关特定问题映像的线索
  • 在调用任何东西之前,请尝试清空缓存,移除所有CacheDimages。这意味着您将从以前的缓存交互中清除,从而耗尽内存池

我认为您的问题不是泄漏,我认为您的问题是保留分配过多。因此,请使用分配工具(带有保留计数跟踪)查看分配了多少内存(VM分配)以及有多少实体保留了这些内存(保留计数).

我已经这样做了,但没有多大帮助,也没有减少内存。有趣的是,下一步我要做的是尝试找出造成问题的代码行。从循环的底部开始,注释掉每一行,然后运行应用程序。重复操作,直到它能够运行。创建
UIImage应该没问题,因为你有自动释放池,但可能有一些事情我没有看到。这不会解决问题,但可能有助于你弄清问题的真相。我也有同样的问题。每次WKInterfaceDevice.currentDevice().addCachedImage()时,内存都会增加调用,直到AwakeWithContext完成后才释放。@BrettHannah在AwakeWithContext完成后,当我弹出时,我正在使用上述代码,内存不会释放?