Image.xcassets
- create
.xcassets
In order toImage Set
Form management pictures, add pictures will generate the correspondingcontent.json
file - join
@2x
和@3x
After isoplogram, after packingAssets.car
Exists in the form of, - use
[UIImage imageNamed:@"xxx"]
To read images, you can use the image cache — essentially creating onekey-value
In the dictionary.key
Is the name of the picture,value
Is the picture object. After the image object is created, the object is added toNSCache
After decodingImage Buffer
Image 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 correspondingUIImage
Objects 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 picture
Resource
Folder, 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
Bundle
The resource file pack, which takes a lot of images,XIB
, text files are organized together and packaged into oneBundle
File to make it easy to reference the resources in the package in other projects.Bundle
The file is static,Does not participate in the compilation of the project.Bundle
Packages cannot contain executable files; they are merely resources that are parsed into specific binary data.- Advantage:
Bundle
In the file does not participate in project compilation, does not affect the size of App package (can be used for App slimming); usebundle
It 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: Use
imageWithContentsOfFile
Read, as follows method 1; Also can beUIImage
Extend it as shown in method 2
- use
imageWithContentsOfFile
read
/// 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
- right
UIImage
Expand 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
xcassets
The pictures inside can only pass throughimageNamed
Load.Bundle
You can also go throughimageWithContentsOfFile
Equal loadingxcassets
In the2x
和3x
, will be distributed on a device basis and will not include both (App Slicing
), andBundle
Will containxcassets
Inside, can be carried on the pictureSlicing
That is, cutting and stretching,Bundle
Does not supportBundle
Support for multiple languages,xcassets
Does not support- In addition, use
imageNamed
To create theUIImage
, will immediately be added toNSCache
After decodingImage Buffer
Unused memory is not released until a memory warning is receivedUIImage
; While the use ofimageWithContentsOfFile
Each 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 inxcassets
Internal management, while large graphs, less frequently used graphs, should be placedBundle
In 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.