加载UIImage的好习惯 [英] Good practice loading UIImage

查看:77
本文介绍了加载UIImage的好习惯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一些图标可以在多个视图中重复使用(例如,收藏夹图标).

I have a few icons in my app that I reuse in multiple views (for example a favorite icon).

为了节省内存,我想知道将带静态图像的类与类方法一起使用是否是一个好主意?

In order to save memory, I was wondering if using a class with class methods which loads static images would be a good idea ?

例如:

+ (UIImage *)favoriteIcon {
    static UIImage * icon;
    if (!icon)
        icon = [UIImage imageNamed:@"favorite.png"];
    return icon;
}

还是我每次需要时都使用+ (UIImage *) imageNamed:(NSString *)name吗?

Or should I just use + (UIImage *) imageNamed:(NSString *)name every time I need ?

谢谢您的建议.

推荐答案

[UIImage imageNamed:]已经在后台进行了类似的操作.实际上,它更聪明,因为它还可以执行一些操作,例如在内存不足且不需要立即使用它们时转储图像,而您的favoriteIcon方法可以使它们永久加载.

[UIImage imageNamed:] is already doing something like this under the hood. In fact, it's smarter, because it is also doing things like dumping the images when memory is low and they aren't needed right away, while your favoriteIcon method keeps them loaded forever.

通常,最好避免进行这样的优化,直到您构建了应用程序,然后对其进行了概要分析以了解其改进方式/位置.否则,您会浪费时间,甚至会使情况变得更糟.我建议您学习如何使用Instruments来配置您的应用程序,这很复杂,但是一旦掌握了它,它就会带来很多乐趣.

In general, it's better to avoid doing optimizations like this until you've built your app and then profiled it to see how/where it needs improvement. Otherwise you are wasting time or maybe even making things worse. I recommend you learn how to use Instruments to profile your app, it's kind of complicated but a lot of fun once you get the hang of it.

这篇关于加载UIImage的好习惯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆