The difference between two types of initialization methods for objects of the UIImage class

Source: Internet
Author: User

1, imagenamed:
1 UIImage *image = [UIImage imagenamed:@ "1"];

class method of UIImage

The first time you read a picture, put this picture in the cache, the next time you use the name of the picture, read directly from the cache, if the cache does not exist in the name of the image, then the picture is loaded into the cache, and then return the object. If we need to load some one-off images frequently in a short time, it is best not to use this method.

Advantages: Convenient and quick, only the first use of the time is slightly slower, the next use will be a little faster;

Cons: If used only once in the current project, it will waste memory.

2, Imagewithcontentsoffile: and Initwithcontentsoffile:
1 //get the path to a picture resource in the package2NSString *oldimagepath = [[NSBundle mainbundle] Pathforresource:@"Image"OfType:@"PNG"];3     //get pictures by Path4UIImage *image1 = [UIImage Imagewithcontentsoffile:oldimagepath];//class Method5UIImage *image2 = [[UIImage alloc] initwithcontentsoffile:oldimagepath];//Object Methods

Imagewithcontentsoffile:--The class method of UIImage

Intiwithcontentsoffile:--The object method of UIImage

Each time the image is read according to the path, it is loaded and returned directly from the file system without the system cache. This method should be selected if the loaded picture is used only once in the project. When a memory warning is received, the system may release the memory of the UIImage internal storage image and reload it the next time it is needed.

The difference between two types of initialization methods for objects of the UIImage class

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.