从应用程序包中加载图像的最佳方法。

时间:2022-02-22 00:25:51

What is the best way of loading images from the application main bundle. I am using

从应用程序主包加载图像的最佳方式是什么?我用

[UIImage imageNamed: "image.png"];

But I have heard that this is not good in terms of memory usage and performance. Can anyone please give his/her feedback on it? My application reads lots of images from main bundle at launch time. I want to make this process as efficient as possible.

但是我听说这在内存使用和性能方面并不好。有人能给他/她的反馈吗?我的应用程序在启动时从主包读取很多图像。我想让这个过程尽可能的高效。

Best Regards

致以最亲切的问候

4 个解决方案

#1


7  

If you want to try loading the images without caching, you can use:

如果您想尝试在没有缓存的情况下加载图像,您可以使用:

[[UIImage alloc] initWithContentsOfFile:@"image.png"]; 

This could be slightly faster loading the image for the first time, since it doesn't have to cache it first. If you only need to use these images once, it probably makes sense to try it this way. The other benefit of doing it this way is that you won't have an image cache using up memory for longer than necessary.

这可能比第一次加载图像要快一些,因为它不必首先缓存图像。如果您只需要使用这些图像一次,那么尝试这种方式可能是有意义的。这样做的另一个好处是,映像缓存的使用时间不会超过需要的时间。

#2


13  

If there were one true "Best Way", the means to load images multiple ways would not exist (unless for historical reasons). Therefore, a little understanding will serve you better than distilling an answer down to a "Best Way".

如果有一种真正的“最佳方式”,就不存在以多种方式加载图像的方法(除非出于历史原因)。因此,一点点的理解将比从“最佳方式”中提炼出一个答案更好。

+[UIImage imageNamed:] caches the image for reuse, and it is a sensible default for most purposes. Caching is excellent if used correctly. The cache is good because it can minimize your disk reads and memory usage by sharing and reusing loaded images, rather than reading and allocating a copy for each image you must display. Consider an icon image which you use on multiple screens - would you like that image data to be read and reallocated each time? This may result in redundant reads and allocations of identical image data. If not, use the caching methods.

+[UIImage imageNamed:]缓存图像以供重用,这是大多数用途的明智默认。如果正确使用,缓存非常好。缓存很好,因为它可以通过共享和重用已加载的映像来最小化磁盘读取和内存使用,而不是为必须显示的每个映像读取和分配副本。考虑你在多个屏幕上使用的图标图像——你是否希望每次都读取和重新分配图像数据?这可能导致重复读取和分配相同的图像数据。如果没有,使用缓存方法。

If you load the image only once and lazily, then you may want consider non-caching approaches.

如果您只加载一次映像并延迟加载,那么可能需要考虑非缓存方法。

  • Image data can consume a lot of memory.
  • 图像数据可以消耗大量内存。
  • Reading an image can take a long time -- not just disk i/o, but also converting it into a usable UIImage representation (e.g. decompressing the image).
  • 读取图像要花很长时间——不仅是磁盘i/o,还需要将其转换为可用的UIImage表示(例如,解压图像)。
  • there are also times where you should resize/scale an image. then you'd want a scaled copy.
  • 也有一些时候你应该调整/缩放一个图像。然后你会想要一个模版。

In short, there are many considerations, but if you have properly scaled assets which you reuse, caching is typically the right choice.

简而言之,有很多考虑因素,但是如果您有适当的可重用资产,缓存通常是正确的选择。

If your assets are not sized properly, then the issue is more fundamental -- you should resize the bundled assets to be appropriate for the purpose if you're experiencing performance problems. Properly sized images also make drawing significantly simpler while retaining the best image quality.

如果您的资产规模不大,那么问题就更基本了——如果您遇到性能问题,您应该调整绑定的资产的大小以适合这个目的。适当大小的图像也使绘制更简单,同时保持最佳的图像质量。

#3


10  

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"myimage" ofType:@"png"];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];

#4


0  

For finding a single resource file using NSBundle

使用NSBundle查找单个资源文件

NSBundle* myBundle = [NSBundle mainBundle];

NSString* myImage = [myBundle pathForResource:@"Seagull" ofType:@"jpg"];

and for finding multiple resources:

寻找多种资源:

NSBundle* myBundle = [NSBundle mainBundle];

NSArray* myImages = [myBundle pathsForResourcesOfType:@"jpg"
                              inDirectory:nil];

#1


7  

If you want to try loading the images without caching, you can use:

如果您想尝试在没有缓存的情况下加载图像,您可以使用:

[[UIImage alloc] initWithContentsOfFile:@"image.png"]; 

This could be slightly faster loading the image for the first time, since it doesn't have to cache it first. If you only need to use these images once, it probably makes sense to try it this way. The other benefit of doing it this way is that you won't have an image cache using up memory for longer than necessary.

这可能比第一次加载图像要快一些,因为它不必首先缓存图像。如果您只需要使用这些图像一次,那么尝试这种方式可能是有意义的。这样做的另一个好处是,映像缓存的使用时间不会超过需要的时间。

#2


13  

If there were one true "Best Way", the means to load images multiple ways would not exist (unless for historical reasons). Therefore, a little understanding will serve you better than distilling an answer down to a "Best Way".

如果有一种真正的“最佳方式”,就不存在以多种方式加载图像的方法(除非出于历史原因)。因此,一点点的理解将比从“最佳方式”中提炼出一个答案更好。

+[UIImage imageNamed:] caches the image for reuse, and it is a sensible default for most purposes. Caching is excellent if used correctly. The cache is good because it can minimize your disk reads and memory usage by sharing and reusing loaded images, rather than reading and allocating a copy for each image you must display. Consider an icon image which you use on multiple screens - would you like that image data to be read and reallocated each time? This may result in redundant reads and allocations of identical image data. If not, use the caching methods.

+[UIImage imageNamed:]缓存图像以供重用,这是大多数用途的明智默认。如果正确使用,缓存非常好。缓存很好,因为它可以通过共享和重用已加载的映像来最小化磁盘读取和内存使用,而不是为必须显示的每个映像读取和分配副本。考虑你在多个屏幕上使用的图标图像——你是否希望每次都读取和重新分配图像数据?这可能导致重复读取和分配相同的图像数据。如果没有,使用缓存方法。

If you load the image only once and lazily, then you may want consider non-caching approaches.

如果您只加载一次映像并延迟加载,那么可能需要考虑非缓存方法。

  • Image data can consume a lot of memory.
  • 图像数据可以消耗大量内存。
  • Reading an image can take a long time -- not just disk i/o, but also converting it into a usable UIImage representation (e.g. decompressing the image).
  • 读取图像要花很长时间——不仅是磁盘i/o,还需要将其转换为可用的UIImage表示(例如,解压图像)。
  • there are also times where you should resize/scale an image. then you'd want a scaled copy.
  • 也有一些时候你应该调整/缩放一个图像。然后你会想要一个模版。

In short, there are many considerations, but if you have properly scaled assets which you reuse, caching is typically the right choice.

简而言之,有很多考虑因素,但是如果您有适当的可重用资产,缓存通常是正确的选择。

If your assets are not sized properly, then the issue is more fundamental -- you should resize the bundled assets to be appropriate for the purpose if you're experiencing performance problems. Properly sized images also make drawing significantly simpler while retaining the best image quality.

如果您的资产规模不大,那么问题就更基本了——如果您遇到性能问题,您应该调整绑定的资产的大小以适合这个目的。适当大小的图像也使绘制更简单,同时保持最佳的图像质量。

#3


10  

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"myimage" ofType:@"png"];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];

#4


0  

For finding a single resource file using NSBundle

使用NSBundle查找单个资源文件

NSBundle* myBundle = [NSBundle mainBundle];

NSString* myImage = [myBundle pathForResource:@"Seagull" ofType:@"jpg"];

and for finding multiple resources:

寻找多种资源:

NSBundle* myBundle = [NSBundle mainBundle];

NSArray* myImages = [myBundle pathsForResourcesOfType:@"jpg"
                              inDirectory:nil];