Best way of loading images from application bundle

前端 未结 4 734
既然无缘
既然无缘 2021-02-07 09:20

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

相关标签:
4条回答
  • 2021-02-07 09:55

    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.

    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).
    • 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.

    0 讨论(0)
  • 2021-02-07 09:57

    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.

    0 讨论(0)
  • 2021-02-07 10:00
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"myimage" ofType:@"png"];
    UIImage *image = [UIImage imageWithContentsOfFile:filePath];
    
    0 讨论(0)
  • 2021-02-07 10:15

    For finding a single resource file using 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];
    
    0 讨论(0)
提交回复
热议问题