首页 \ 问答 \ 从superview中删除UIImageView后未释放内存(Memory not released after removing UIImageView from superview)

从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
更新时间:2023-05-05 19:05

最满意答案

我终于与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_ ...
  • 而不是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.
  • 我没有看到任何原因会抛出“ProductNotFoundException” if (!allProducts.isEmpty()) { //Code } else { System.out.println("No such product " + product.getId()); throw new ProductNotFoundException(product.getName()); } 显然,如果你的allProducts不为空,它将永远不会抛出异 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)