iphone - IOS/iPhone基于 slider 值内存问题将图像加载到UIImage中

标签 iphone cocoa-touch ios ipad

我正在使用 slider 的值将图像加载到 UIImage 中(如果您愿意,这些是页面或幻灯片)。这个 UIImage 使用 slider 非常快速地切换它们。问题是,在某些时候,应用程序在设备上崩溃并出现以下错误:

2011-04-02 17:39:01.836 Book1[2123:307] 收到内存警告。等级=1

这是代码:

- (IBAction)slidePages:(id)sender{
    int sliderValue = pageSlider.value;

    NSString *slideToPage = [NSString stringWithFormat:@"P%i.jpg", sliderValue];
    imagePlaceholder.image = [UIImage imageNamed:slideToPage];

    pageDisplay.text = [NSString stringWithFormat:@"Page %i", sliderValue];

    currentPage = sliderValue;  
}

我能做些什么来提高效率吗?也许错误在其他地方,但我猜它与图像的快速加载有关。

不过,我不知道 iOS 是如何处理这个问题的。每次我将新图像加载到 UIImage 中时,“未加载”的图像会发生什么?

提前致谢。

最佳答案

[UIImage imageNamed] 的一个重要方面是它缓存了所有以这种方式加载的图像并且它们永远不会被卸载,即使你释放了创建的 UIImage !这在某些情况下很好(例如 UITableView 单元格中的小图像),但在其他情况下不好(例如大图像)。

解决方法是使用 [UIImage imageWithData]它不执行此缓存,并且在 UIImage 被释放时卸载数据。

更多信息和讨论在这里:

Difference between [UIImage imageNamed...] and [UIImage imageWithData...]?

更新

This question关于 [UIImage imageNamed:] 的问题有一些很好的信息发生内存警告时不清空其缓存。

关于iphone - IOS/iPhone基于 slider 值内存问题将图像加载到UIImage中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5526218/

相关文章:

ios - 用于生产应用程序的 Crashlytics CLS_Log 与 NSLog

ios - Swift框架与Cocoapods——整合静态库

iOS _OBJC_CLASS_$_ASIdentifierManage 引用自 : objc-class-ref in ViewController. o

iphone - iphone 可以多快的 fps 截取屏幕截图?

iphone - AVErrorMediaServicesWereReset 是什么意思?

ios - 使用 Youtube API 搜索 channel

ios - 如何删除UIImage背景颜色并使之透明?

ios - 如何在 iOS 中缓存 NSDATA json

iphone - 使用设备运动代替加速度计、陀螺仪和磁力计

iphone - 通过 WIFI 连接到 Safari Web Inspector