Two ways IOS loads an image

Source: Internet
Author: User

Apple's official documentation provides two ways to load an image

+ (Nullable UIImage *) imagenamed: (NSString *) name;

+ (Nullable UIImage *) Imagewithcontentsoffile: (NSString *) path;

So what's the difference between these two methods for loading a picture, let's illustrate the difference between the two methods with a sequence frame animation:

This program loads three sets of sequence frame animations altogether:

First we use + (nullable UIImage *) imagenamed: (NSString *) name; to load a picture

When we run the program, look at the program memory:

When I click on the first group, I look at the memory

Next, I continue to click on the second group to play the animation of the second set of sequence frames

Next I click on the third group to play the third set of sequence frame animations

So then we're going to analyze why memory is growing so much, and it won't be released when the program runs.

When executing self.imageView.animationImages = Nmarray, a strong reference is made to the array so that the contents of the arrays are not freed. So the picture will always be in memory and will not be released.

When I clicked on another set of frame images, the array would point to another set of images, and the previously pointed image would have been released. If this is the case, the memory will not continue to rise, but the current array

The memory size of a group of pictures pointed to. The actual phenomenon is that this memory has been increasing, so when animationimages this pointer to another image array, the image array it previously pointed to is not released in memory,

Instead, it was put into the buffer. Will cause the memory to increase, the picture will not be released. Obviously, when we want the array to point to another array of images, we want to free up memory by releasing the array of previously pointed images.

There are two different ways to load images. Next let's take a look at the + (nullable UIImage *) Imagewithcontentsoffile: (NSString *) path; this way to load the picture,

Look at the memory effect again

Just changed a picture loading method, the other code did not make any changes. Next look at the memory

Also when I click on the first set of sequence frames,

When I point to the second set of sequence frame animations:

When I click on the third set of images:

The difference between the two methods of loading pictures, in terms of memory, looks obvious.

So what's the difference between the two ways to load a picture? Above we said when using + (Nullable UIImage *) imagenamed: (NSString *) name this way when loading a picture, when

When the picture is not pointing, it is not released, but is placed in the buffer, so the memory will always increase. While using + (nullable UIImage *) Imagewithcontentsoffile: (NSString *) path

When the image is loaded in this way, it will be released when there is no pointer to the image. So the memory will not always increase.

So, the two ways to load pictures depends on different scenarios. When the picture will be used multiple times, and the picture is small, we use + (nullable UIImage *) imagenamed: (NSString *) name

This way load, because the picture is cached in the cache, the next time you use the picture, the efficiency of the load will increase. When the picture is not used frequently, and the picture is larger, use + (nullable UIImage *) Imagewithcontentsoffile: (NSString *) path this way load, when the picture is used, the direct release out. Because the picture is basically not reused, the user rarely looks at a set of sequence frames to be bullish several times.

The difference between the two loading images is obvious through the memory comparison.

Next, there is a small problem, that is, when a set of sequence frames play, if you do not click on another sequence frame animation, this array will always point to the image array. This will cause the last set of sequence frame pictures to not be released,

There is always a strong pointer pointing.

This time the sequence frame animation is finished, and the memory is still 280M, so when the sequence frame animation is finished, we have to put self.imageView.animationImages this pointer to nil. and release the image.

In this way, the memory problem for the sequence frame animation has all been resolved.

Summary: When the picture to be used frequently, and the picture is relatively small, we use imagename way to load, such as Tableviewcell inside the small icon, or button picture.

When the picture is not used frequently, the use of imagewithcontentoffile to load, the image after the end of the use of direct release, there is no need to continue to account for the memory. such as sequence frame animation

Two ways IOS loads an image

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.