SoFunction
Updated on 2025-04-13

Implementation of three ways of storing images in IOS

  • Create .xcassets to manage images in the form of Image Set. After adding images, the corresponding file will be generated.
  • After adding the equal-fold graphs of @2x and @3x, it will be packaged and exist in the form of .
  • Use [UIImage imageNamed:@"xxx"] to read the image, and you can use the image cache - it is equivalent to creating a key-value dictionary, with key as the image name and value as the image object. After creating the image object, the object is added to the NSCache (decoded Image Buffer), and the image object that is not in use will not be released until the memory warning is received. Therefore, for images that need to be displayed in multiple places, their corresponding UIImage objects will only be created once (regardless of the recycling of memory warnings), reducing memory consumption.

The picture is directly added to the project as a Resource

Reading method: Create a picture Resource folder, add the picture directly to the project, and read the picture using the following method

NSString *path = [ pathForResource:@"xxx" type:@"png"];
UIImage *image = [UIImage imageWithContentsOfFile:path];

Features: In the image management method of Resource, all image creation is obtained by reading file data. Reading file data once will generate NSData once and a UIImage will be generated. When the image is created, the corresponding NSData will be destroyed. When the reference counter of the UIImage becomes 0, the UIImage will be automatically destroyed. This will ensure that the image will not exist in memory for a long time.

Usage scenario: Due to the characteristics of this method, the Resource method is generally used when the image data is large and the image generally does not need to be used multiple times, such as the background of the guide page (full screen of the picture)

Advantages: The pictures will not be stored in memory for a long time, so there will be no waste of memory. At the same time, large images are generally not used for a long time, and large images generally take up many times more memory than small images. Therefore, in reducing the memory usage of large images, Resource does a very good job

Using Bundle File

  • Bundle is a resource file package, which organizes many pictures, XIB, and text files together and packages them into a Bundle file to facilitate reference to the resources in the package in other projects.
  • The Bundle file is static and does not participate in the compilation of the project. The Bundle package cannot contain executable files. It is just used as a resource and is parsed into specific binary data.
  • Advantages: Files in Bundle do not participate in project compilation and do not affect the size of the App package (can be used for app slimming); use the bundle method to facilitate the management of files, and facilitate reference to resources in packages in other projects.
  • Use scenarios: larger images, or images with lower frequency usage
  • Reading method: Use imageWithContentsOfFile to read, as follows method 1; you can also expand UIImage, as follows method 2

Read with imageWithContentsOfFile

/// 

// bundle path
#define STBundle_Name @""
#define STBundle_Path [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:STBundle_Name]
#define STBundle [NSBundle bundleWithPath:STBundle_Path]
/// usage

#import ""
UIImageView * headerBgImgView = [[UIImageView alloc] init];
 = [UIImage imageWithContentsOfFile:[SecKill_BUNDLE pathForResource:@"xxxx" ofType:@"png"]];

Extend UIImage and create the UIImage+BSKResources class

/// UIImage+

NS_ASSUME_NONNULL_BEGIN

@interface UIImage (BSKResources)

+ (UIImage *)bskImageNamed:(NSString *)imageName InBundleName:(NSString *)bundleName;

@end

NS_ASSUME_NONNULL_END
/// UIImage+

#import "UIImage+"

@implementation UIImage (BSKResources)

+ (UIImage *)bskImageNamed:(NSString *)imageName InBundleName:(NSString *)bundleName
{
  NSString *resourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:bundleName];
  NSBundle *resourceBundle = [NSBundle bundleWithPath:resourcePath];
  return [UIImage imageNamed:imageName inBundle:resourceBundle compatibleWithTraitCollection:nil];
}
@end

/// usage

#import "UIImage+"

UIImageView * headerBgImgView = [[UIImageView alloc] init];
 = [UIImage bskImageNamed:@"xxx" InBundleName:@""]];

Difference between Bundle and xcassets

  • The images in xcassets can only be loaded through imageNamed. Bundle can also be loaded through imageWithContentsOfFile and other methods
  • 2x and 3x in xcassets will be distributed according to the specific device and will not include both (App Slicing), while Bundle will both include
  • In xcassets, images can be slicing, that is, cropping and stretching, but Bundle does not support them
  • Multilingual support is supported in Bundle, xcassets does not support
  • In addition, the UIImage created with imageNamed will be immediately added to the NSCache (decoded Image Buffer). The UIImage that is not in use will not be released until the memory warning is received; the objects created with imageWithContentsOfFile will be re-applyed for memory every time, and the same image will not be cached. Therefore, it is recommended that commonly used and smaller figures be managed in xcassets, while large figures and low-frequency usage should be managed in Bundle

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.