Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize picture storage by IOS

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "IOS how to achieve picture storage", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "IOS how to achieve picture storage" this article.

Image.xcassets

Create .xcassets to manage images in the form of Image Set. After adding images, you will generate corresponding content.json files, add @ 2x and @ 3x magnification, and then package them in the form of Assets.car. Use [UIImage imageNamed:@ "xxx"] to read images. You can use image cache-equivalent to creating a dictionary of key-value, with key as the image name and value as the image object. After the picture object is created, it is added to the NSCache (the decoded Image Buffer), and the picture object that is not in use is not released until a memory warning is received. Therefore, for images that need to be displayed in multiple places, their corresponding UIImage objects are created only once (regardless of recycling when memory warnings are taken into account), reducing memory consumption.

The picture is directly added to the project as Resource.

How to read: create a picture Resource folder, add the picture directly to the project, and read the picture in the following way

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

Features: in Resource's image management mode, all image creation is obtained by reading file data, which generates a NSData and a UIImage after reading the file data. Destroy the corresponding NSData when the picture is created, and automatically destroy the UIImage when the reference counter of the UIImage changes to 0, so that the picture will not be stored in memory for a long time.

Usage scenario: due to the characteristics of this method, the Resource method is generally used in situations where the image data is very large and the picture does not need to be used many times, such as the background of the boot page (full screen).

Advantages: pictures will not be kept in memory for a long time, so there will not be a lot of memory waste. At the same time, large images generally do not use for a long time, and large images generally take up many times more memory than small ones, so Resource does a very good job in reducing the memory footprint of large images.

Working with Bundle files

Bundle is a resource package, which organizes many pictures, XIB, and text files together and packages them into a Bundle file, making it easy to reference 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 simply parsed as a resource into specific binary data. Advantages: files in Bundle do not participate in project compilation and do not affect the size of the App package (which can be used to reduce the size of App); use bundle to facilitate file management and facilitate referencing resources in the package in other projects. Use scenarios: larger pictures, or less frequently used picture reading methods: use imageWithContentsOfFile to read, as shown in method 1; you can also extend UIImage, as shown in method 2

Use imageWithContentsOfFile to 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]

/ / usage#import "BSKDefine.h" UIImageView * headerBgImgView = [[UIImageView alloc] init]; headerBgImgView.image = [UIImage imageWithContentsOfFile: [Seckill _ BUNDLE pathForResource:@ "xxxx" ofType:@ "png"]]

Extend UIImage to create a UIImage+BSKResources class

/ / UIImage+BSKResources.hNS_ASSUME_NONNULL_BEGIN@interface UIImage (BSKResources) + (UIImage *) bskImageNamed: (NSString *) imageName InBundleName: (NSString *) bundleName;@endNS_ASSUME_NONNULL_END

/ / 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

/ / usage#import "UIImage+BSKResources.h" UIImageView * headerBgImgView = [[UIImageView alloc] init]; headerBgImgView.image = [UIImage bskImageNamed:@ "xxx" InBundleName:@ "BSKResources.bundle"]]

The difference between Bundle and xcassets

The pictures in xcassets can only be loaded through imageNamed. Bundle can also load 2x and 3x in xcassets through imageWithContentsOfFile and other methods, which will be distributed according to specific devices and will not be included at the same time (App Slicing), while Bundle will be included in xcassets, and images can be Slicing, that is, cropping and stretching. Bundle does not support multiple languages in Bundle, and xcassets does not support it. In addition, UIImage created using imageNamed will be immediately added to NSCache (decoded Image Buffer) until a memory warning is received. To release UIImage that is not in use Objects created with imageWithContentsOfFile will rerequest memory each time, and the same images will not be cached. Therefore, it is suggested that the commonly used and smaller diagrams should be managed in xcassets, while larger diagrams and less frequently used diagrams should be managed in Bundle.

The above is all the contents of the article "how to store pictures in IOS". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report