For the difference between the two initialization methods of the UIImage Class Object, uiimage Initialization

Source: Internet
Author: User

For the difference between the two initialization methods of the UIImage Class Object, uiimage Initialization
1. imageNamed:

1 UIImage *image = [UIImage imageNamed:@"1"];

Class Method of UIImage

When reading an image for the first time, put the image in the cache and then read the image with the name directly from the cache. If the image with the name does not exist in the cache, load the image to the cache before returning the object. If we need to frequently load some disposable images in a short period of time, we 'd better not use this method.

Advantage: it is convenient and quick. It is only a little slow when used for the first time. It will be faster when used later;

Disadvantage: memory will be wasted if it is used only once in the current project.

 

2. imageWithContentsOfFile: And initWithContentsOfFile:
1 // obtain the path of an image resource in the package. 2 NSString * oldImagePath = [[NSBundle mainBundle] pathForResource: @ "image" ofType: @ "png"]; 3 // obtain the image by Path 4 UIImage * image1 = [UIImage imageWithContentsOfFile: oldImagePath]; // Class Method 5 UIImage * image2 = [[UIImage alloc] initWithContentsOfFile: oldImagePath]; // object Method

ImageWithContentsOfFile: -- class method of UIImage

IntiWithContentsOfFile: -- Object method of UIImage

Each time the image is read Based on the path, it is directly loaded from the file system and returned without going through the system cache. It does not consume memory. If the loaded image is used only once in the project, select this method. When receiving a memory warning, the system may release the memory for storing images in the UIImage and re-load the image as needed next time.

 

 

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.