Image.xcassets

  • create.xcassetsIn order toImage SetForm management pictures, add pictures will generate the correspondingcontent.jsonfile
  • join@2x@3xAfter isoplogram, after packingAssets.carExists in the form of,
  • use[UIImage imageNamed:@"xxx"]To read images, you can use the image cache — essentially creating onekey-valueIn the dictionary.keyIs the name of the picture,valueIs the picture object. After the image object is created, the object is added toNSCacheAfter decodingImage BufferImage objects that are not in use are not released until a memory warning is received. Therefore, for images that need to be displayed in more than one place, the correspondingUIImageObjects are created only once (without regard to the collection of memory warnings), reducing memory consumption.

Images are added directly to the project as resources

  • Read: Create a pictureResourceFolder, directly add the image to the project, use the following method to read the image
NSString *path = [NSBundle.mainBundle pathForResource:@"xxx" type: @"png"];
UIImage *image = [UIImage imageWithContentsOfFile:path];
Copy the code
  • Feature: In the image management mode of Resource, all images are created by reading file data. Reading file data generates an NSData and a UIImage. The NSData is destroyed when the image is created, and the UIImage is automatically destroyed when the reference counter of the UIImage goes to zero, so that the image is guaranteed not to be in memory for long

  • Usage scenarios: Due to the nature of this method, the Resource method is usually used in situations where the image data is large and the image does not need to be used many times, such as the background of the guide page (image full screen).

  • Advantage: Images are not stored in memory for a long time, so there is not a lot of memory waste. At the same time, the large map is generally not used for a long time, and the large map occupies many times more memory than the small map, so in reducing the memory footprint of the large map, Resource does a very good job

Using Bundle files

  • BundleThe resource file pack, which takes a lot of images,XIB, text files are organized together and packaged into oneBundleFile to make it easy to reference the resources in the package in other projects.
  • BundleThe file is static,Does not participate in the compilation of the project.BundlePackages cannot contain executable files; they are merely resources that are parsed into specific binary data.
  • Advantage:BundleIn the file does not participate in project compilation, does not affect the size of App package (can be used for App slimming); usebundleIt is convenient to manage files and reference the resources in the package in other projects.
  • Use scenarios: larger images, or less frequently used images
  • Read mode: UseimageWithContentsOfFileRead, as follows method 1; Also can beUIImageExtend it as shown in method 2
  1. useimageWithContentsOfFileread
/// BSKDefine.h

// bundle path
#define STBundle_Name @"SafeToolResource.bundle"
#define STBundle_Path [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:STBundle_Name]
#define STBundle [NSBundle bundleWithPath:STBundle_Path]
Copy the code
/// usage

#import "BSKDefine.h"
UIImageView * headerBgImgView = [[UIImageView alloc] init];
headerBgImgView.image = [UIImage imageWithContentsOfFile:[SecKill_BUNDLE pathForResource:@"xxxx" ofType:@"png"]].Copy the code
  1. rightUIImageExpand to createUIImage+BSKResources
/// UIImage+BSKResources.h

NS_ASSUME_NONNULL_BEGIN

@interface UIImage (BSKResources)

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

@end

NS_ASSUME_NONNULL_END
Copy the code
/// UIImage+BSKResources.m

#import "UIImage+BSKResources.h"

@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
Copy the code
/// usage

#import "UIImage+BSKResources.h"

UIImageView * headerBgImgView = [[UIImageView alloc] init];
headerBgImgView.image = [UIImage bskImageNamed:@"xxx" InBundleName:@"BSKResources.bundle"]].Copy the code

Bundle and Xcassets

  • xcassetsThe pictures inside can only pass throughimageNamedLoad.BundleYou can also go throughimageWithContentsOfFileEqual loading
  • xcassetsIn the2x3x, will be distributed on a device basis and will not include both (App Slicing), andBundleWill contain
  • xcassetsInside, can be carried on the pictureSlicingThat is, cutting and stretching,BundleDoes not support
  • BundleSupport for multiple languages,xcassetsDoes not support
  • In addition, useimageNamedTo create theUIImage, will immediately be added toNSCacheAfter decodingImage BufferUnused memory is not released until a memory warning is receivedUIImage; While the use ofimageWithContentsOfFileEach time the object is created, the memory will be reallocated, and the same image will not be cached. Therefore, it is recommended that the commonly used, smaller diagrams be placed inxcassetsInternal management, while large graphs, less frequently used graphs, should be placedBundleIn the management

Image resource file size calculation

  • Extended Reading – Dry goods! Jingdong Mall iOS App slimming practice

In daily development, App package size will be optimized and slimmed down. In the past, it was common to assume that the file size of a resource in a local development project was the size of the final installation package, and that local images could be reduced by compressing them.

And according to dry goods! Article point of view, many pictures in the local and installation package size difference is very large, often several times, even dozens of times. Through investigation, we know that Apple converts PNG to CgBI non-standard PNG format in order to optimize the reading speed of PNG images on iPhone devices

  • extra critical chunk (CgBI)
  • byteswapped (RGBA -> BGRA) pixel data, presumably for high-speed direct blitting to the framebuffer
  • zlib header, footer, and CRC removed from the IDAT chunk
  • premultiplied alpha (color’ = color * alpha / 255)

Apple’s optimization is a negative optimization of package size for most apps, and Mall is no exception. Therefore, simple compression (lossy, lossless) processing can not achieve a good weight loss effect. After testing, the following files will be negatively optimized

  • PNG image in the root directory
  • PNG, JPG images in the Asset Catalog, where JPG is converted to PNG

JPG files in the root directory, PNG files in the bundle are not optimized, and the size of the local image is the size of the resource file in the installation package.

other

Capture the middle part of the picture to show

In business development, there is a need to capture the middle part of the picture for display, as shown in the figure above. Record its implementation as follows

Self. topBgImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,100,100)]; // Show the middle part of the image [_topBgImgViewsetContentScaleFactor:[[UIScreen mainScreen] scale]];
_topBgImgView.contentMode =  UIViewContentModeScaleAspectFill;
_topBgImgView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
_topBgImgView.clipsToBounds  = YES;
Copy the code

Reference IOS about UIImageView display – centered or take pictures in the middle of the part of a display | CSDN learn more.