【问题标题】:Large amounts of memory allocated on setImage:在 setImage 上分配的大量内存:
【发布时间】:2014-08-29 00:32:24
【问题描述】:

我有一个以编程方式创建的 UIImageView ([[UIImageView alloc] init])。在调用setImage: 方法之前,应用程序的内存一直处于检查状态。有什么想法吗?

【问题讨论】:

    标签: ios objective-c iphone memory


    【解决方案1】:

    我假设您正在使用以下方式将图像设置为图像视图:

    [imgV setImage:[UIImage imageNamed:@"yourImg.png"]]
    

    使用它的问题是应用缓存这些图像。如果您想避免缓存图片,请使用imageWithContentsOfFile:

    [imgV setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"yourImg.png" ofType:nil]]];
    

    另外,请务必在使用完毕后将您的图片设置为nil

    [imgV setImage:nil];
    

    我过去曾遇到过这方面的问题,以下是我从 Apple 回复 TSI 的电子邮件中的一些文本:

    在很多情况下,您都在使用 API UIImage +imageNamed: 加载图像,但你应该知道,即使在返回的 UIImage 对象是之后,imageNamed 也会缓存其图像数据 释放。用 -imageWithContentsOfFile 替换对 imageNamed 的调用: 如下所述是一种确保完全控制您的应用程序的方法 内存中的图像数据

    【讨论】:

    • 我正在使用 [UIImage imageWithData:]
    • 您是否在完成后将图像视图的image 设置为nil?您正在加载的图像有多大(以像素为单位)?
    • 我要等一下:|
    猜你喜欢
    • 2012-09-28
    • 1970-01-01
    • 1970-01-01
    • 2019-02-19
    • 1970-01-01
    • 2011-03-20
    • 2010-12-30
    • 2011-11-03
    相关资源
    最近更新 更多