从superview中删除UIImageView后未释放内存(Memory not released after removing UIImageView from superview)
在iOS 6.1上,我有一个带有scrollView的视图控制器,当用户在页面中轻弹时,我需要显示一些图像。 scrollView有3个子视图,分别显示当前图像,上一图像和下一图像。 在滚动期间替换图像。 该机制工作正常,但资源未按预期释放,并且在滚动大约15个图像后终止应用程序。
起初我尝试解决问题只是将新图像分配给UIImageView,但它不起作用(我读到图像被缓存),所以我尝试了一种不同的方法:
// access the UIImageView of the currentView for (UIView *subviewOfCurrentView in [currentView subviews]) { if ([subviewOfCurrentView isKindOfClass:[UIImageView class]]) { UIImageView *cv = (UIImageView *)subviewOfCurrentView; //cv.image = nil; [cv removeFromSuperview]; UIImageView *new_cv = [[UIImageView alloc] initWithFrame:photoRect]; new_cv.image = [UIImage imageNamed:[myObject getFileName]; [currentView addSubview:new_cv]; } }
使用活动监视器对应用程序进行概要分析表明,当我轻弹图像时,实际内存使用量会持续增长,尽管子视图的数量保持不变。
请注意,应用程序终止而不调用didReceiveMemoryWarning。
如何强制我的应用程序释放UIImage(s)和UIImageView(s)使用的内存?
如果你能帮助我,非常感谢。
On iOS 6.1, I have a view controller with a scrollView in which I need to display some images as the user flicks through the pages. The scrollView has 3 subviews to display the current, the previous and the next image respectively. Images are substituted during the scrolling. The mechanism works fine, but resources are not released as expected and the app is terminated after scrolling approximately 15 images.
At first I tried solving the problem simply assigning the new image to the UIImageView, but it did not work (I read that images are cached), so I tried a different approach:
// access the UIImageView of the currentView for (UIView *subviewOfCurrentView in [currentView subviews]) { if ([subviewOfCurrentView isKindOfClass:[UIImageView class]]) { UIImageView *cv = (UIImageView *)subviewOfCurrentView; //cv.image = nil; [cv removeFromSuperview]; UIImageView *new_cv = [[UIImageView alloc] initWithFrame:photoRect]; new_cv.image = [UIImage imageNamed:[myObject getFileName]; [currentView addSubview:new_cv]; } }
Profiling the app with the Activity Monitor shows that the Real Memory Usage keeps growing when I flick an image, despite the fact that the number of subviews remains constant.
Note that the app is terminated without calling didReceiveMemoryWarning.
How can I force my app to release the memory used by UIImage(s) and UIImageView(s)?
If you can help me, much appreciated.
原文:https://stackoverflow.com/questions/18893455
最满意答案
我终于与OkHttp团队讨论了,现在修复程序在当前版本中!
OkHttp v2.5.0
OkIO v1.6.0
更多信息请访问: https : //github.com/square/okhttp/pull/1817
根本原因是android 4错误: https : //code.google.com/p/android/issues/detail?id = 54072
I finally discuss with the OkHttp Team, And now the fix is inside the current release !
OkHttp v2.5.0
OkIO v1.6.0
More information here : https://github.com/square/okhttp/pull/1817
Root cause is a android 4 bug : https://code.google.com/p/android/issues/detail?id=54072
相关问答
更多-
error也不例外。 PHP本身不会在语法错误上抛出异常,例如缺少函数。 你需要set_error_handler()来“捕获”这样的东西 - 即使这样,自定义错误处理程序也无法处理这样的解析错误。 error is not an exception. PHP itself doesn't throw exceptions on syntax errors, such as missing functions. You'd need set_error_handler() to "catch" such t ...
-
除非您在函数(代码中的Main函数[HandleProcessCorruptedStateExceptions]上指定属性[HandleProcessCorruptedStateExceptions] ,否则某些异常不会被try..catch块捕获。 当然,同样可以通过修改Oxoron描述的配置文件来完成。 Some Exceptions are not getting caught by try..catch blocks unless you specify the attribute [HandleP ...
-
我终于解决了。 正如您所说,文件的末尾已在流中到达。 为了解决这个问题,我抓住了Xml根节点,并在所有后续XPath.evaluate调用中使用它而不是源代码。 这样我第一次调用后就不需要打开流来评估我抓住根节点的位置了! 死了chuffed我解决了:) I have finally worked it out. As you say the end of the file was reached in the stream. To get around this I grab the Xml root n ...
-
这绝对是一种代码味道。 在方法的末尾重新抛出异常将覆盖异常的调用堆栈,以便显示所有异常发生在方法的末尾而不是它们真正发生的位置。 如果你不能把最后的代码放在现有的代码中,你可以创建嵌套的try..finally块: try { try { //something } finally { DoSomething(); } ... //more code } finally { // cleanup code } 注意来自OP:请参阅此代码 ,了解此答案的作者 ...
-
我终于与OkHttp团队讨论了,现在修复程序在当前版本中! OkHttp v2.5.0 OkIO v1.6.0 更多信息请访问: https : //github.com/square/okhttp/pull/1817 根本原因是android 4错误: https : //code.google.com/p/android/issues/detail?id = 54072 I finally discuss with the OkHttp Team, And now the fix is inside t ...
-
while ((input = fis.read(buffer)) != -1) { // <<-- STACK TRACE ERROR fos.write(buffer, 0, input); 要么必须是} ,对吧? 或者将执行fis.close ,并且将发生错误。 Log.i("<<<<<>>>>", "fos2 is is; " + buffer[7]); try { fos.flush(); fos.close(); fis.close(); } catch ( ...
-
传递:close_others => false作为选项,除了设置close_on_exec = false解决它。 例如 exec "./client.rb", f.fileno.to_s, :close_others => false Passing :close_others => false as options in addition to setting close_on_exec = false solves it. E.g. exec "./client.rb", f.fileno.to_ ...
-
okhttp替代容器(okhttp alternative container)[2021-06-28]
而不是BasicNameValuePair你可以使用Pair getKey()的等价物将是pair.first 。 getValue()的一个是pair.second instead of BasicNameValuePair you can use a Pair the equivalent for getKey() would be pair.first. The one for getValue() would be pair.second -
你只能使用response.body()。string()一次。 我曾打过两次电话。 首先记录响应,然后再记录json。 You can only use response.body().string() only once. I was calling it twice. First for logging the response and then again for json casting.
-
无法捕获异常(Unable to catch the exception)[2023-04-10]
我没有看到任何原因会抛出“ProductNotFoundException” if (!allProducts.isEmpty()) { //Code } else { System.out.println("No such product " + product.getId()); throw new ProductNotFoundException(product.getName()); } 显然,如果你的allProducts不为空,它将永远不会抛出异 ...