IOS correctly Select how images are loaded

Source: Internet
Author: User

The correct choice of image loading mode can play a big role in memory optimization, common image loading methods have the following three kinds:
Method 1  UIImage *imag1 = [UIImage imagenamed:@ "Image.png"];  Method 2  UIImage *image2 = [UIImage imagewithcontentsoffile:[[nsbundle mainbundle] pathforresource:@ "Image.png" Oftype:nil]];  Method 3  NSData *imagedata = [NSData datawithcontentsoffile:[[nsbundle mainbundle] pathforresource:@ "Image.png" Oftype:nil]];  UIImage *image3 = [UIImage imagewithdata:imagedata];  
The first method: imagenamed:

Why are there two ways to do the same thing? The advantage of imagenamed is that you can cache images that have already been loaded. Apple's documentation has the following statement:

This method looks in the system caches for a image object with the specified name and returns that object if it exists. If a matching Image object is not already in the cache, this method loads the image data from the specified file, caches I T, and then returns the resulting object.

This method will first look for the image in the system cache according to the specified name, and return if found. If a picture is not found in the cache, the method loads the picture data from the specified file, caches it, and then returns the result. For the same image, the system will only cache it to memory once, which is very advantageous for the reuse of images. For example: You need to reload the same icon in a tableview, then load the image with Imagenamed, the system will cache the icon to memory, in the table every time the image is used, only the picture pointer to the same piece of memory. In this case, loading the image using imagenamed becomes very effective.

However, it is therefore used to imageNamed cache pictures, the image of the data in memory, iOS memory is very precious and when memory consumption is too large, will force the release of memory, you will encounter memory warnings. Releasing the memory in the iOS system is a hassle and may cause a memory leak. For example, when a UIView object's animationimages is a dynamic array containing UIImage objects Nsmutablearray, and is animated by frames. When using imagenamed to load an image into a dynamic array nsmutablearray, this is likely to cause a memory leak. The reason is obvious.

The second method and the third method are essentially the same: Imagewithcontentsoffile: and Imagewithdata:

imageWithContentsOfFile: Only images are loaded, image data is not cached, and images are loaded into the program by the system in data mode. Therefore, for larger pictures and less usage, you can use this method to reduce memory consumption.

The following is a list of two ways to use the details:

NSString *path = [[NSBundle mainbundle] pathforresource:@ "icon" oftype:@ "PNG"];  UIImage *image = [UIImage Imagewithcontentsoffile:path];  

And:

NSString *filepath = [[NSBundle mainbundle] pathforresource:filename ofType: "PNG"];  NSData *image = [NSData Datawithcontentsoffile:filepath];  UIImage *image = [UIImage imagewithdata:image]; or = [UIImage Imagewithcontentsoffile:filepath];  

If you load a large picture and use it only once, you do not need to cache the image. This is imagewithcontentsoffile more appropriate, and the system does not waste memory to cache pictures.
However, if you often need to reuse images in your program, it is best to choose the Imagenamed method. This method saves the time it takes to load pictures from disk each time.

  

IOS correctly Select how images are loaded

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.