UIImage imageWithContentsOfFile: difference between path and imageNamed.

Source: Internet
Author: User

UIImage imageWithContentsOfFile: difference between path and imageNamed.

This problem was found in the tomcat small example.

+ (UIImage *)imageWithContentsOfFile:(NSString *)path

By loading files in a specified path (full path), you can find and load image data to generate and return a graphical object (or nil). However, it is only the object that loads the image, this graphic object is not cached.

General Usage:

NSString *file = [[NSBundle mainBundle] pathForResource:name ofType:nil];UIImage *image = [UIImage imageWithContentsOfFile:file];

In fact, take a closer look. Obviously, in this document, the official method is described below Creating New Images. Only create a new image object.

Check again

imageNamed:inBundle:compatibleWithTraitCollection:

Under the Cached Image Loading Routines directory, it is in the bundle. By matching a feature (Directly Writing the Image name), an Image is returned, and the pie is Cached.

+ (UIImage *)imageNamed:(NSString *)name               inBundle:(NSBundle *)bundlecompatibleWithTraitCollection:(UITraitCollection *)traitCollection

The parameters are the image name and the package of the image. They are used to describe the set of features of the image to be generated. This method is a new feature of ios8! If no match is found, nil is returned.

After the image object is successfully generated, if the generated image cache does not exist in the memory, this method will still locate and load the image data in the disk or other resources and return it. Therefore, the cache always exists!

This method is thread-safe!

Here we will look at this

UITraitCollection 

This class is a collection class for view Manager features and details storage, such as proportions and sizes. When a View Controller is generated, this set automatically generates an object for this view controller. Of course, some other classes, such as the UIImage class, also have similar functions to save the same features.

 

One Parameter

+ (UIImage *)imageNamed:(NSString *)name

The returned image object (or nil) is cached. If the image is loaded for the first time, this method searches for the image in the app's main package by name. This is why, when you drag an image to a project, you usually choose

1> Destination: select

2> Folders:

Different, then the method used is restricted.

Because the first item is selected: The generated yellow folder (differentiation point), which is divided into folders in Xcode, but all the materials in Bundle are in the same folder, the development efficiency is very high. Therefore, [NSBundle mainBundle] can be directly used as the resource path, which is highly efficient! You can use [UIImage imageNamed:] to load images. Select the second item: generate a blue folder (feature), and split the folder in Xcode and the Bundle into folders. Therefore, you can duplicate the file name, the actual path needs to be spliced on the basis of [NSBundle mainBundle], which is less efficient! You cannot use [UIImage imageNamed:] to load images.

 

The thread is also insecure.

 

Generally speaking, if large images are not commonly used, use the imagewithcontentsoffile method, which is relatively small (comparison mark) and frequently used. Use the latter and imageNamed for loading. Memory resident, high efficiency. However, for example, in tomcat, so many images are loaded with imagenamed, which can easily cause memory leakage and program crash. You also need to disable the animation and clear the Image array in time.

 

Now, let's take a look at new discoveries.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.