Differences between imageNamed and imageWithContentsOfFile)

Source: Internet
Author: User

For example, do you want to load images for convenience?

MyImage = [UIImage imageNamed: @ "icon.png"];
So be careful
This method is OK in programs with few images or small images.
However, do not do this in a large number of image loading programs.
Why ???????

This method is used to search for images with supplied names in the top-level folder of application bundle. If you find the image, load it to the iPhone system to cache the image. That means that the image is stored in the memory as a cache (theoretically.

What is the consequence of having too many images?
The image cache may not respond to memory warnings and release its objects.

Therefore, you must be careful with alloc and release when using images.
Recommended
NSString * path = [[NSBundle mainBundle] pathForResource: @ "icon" ofType: @ "png"];
MyImage = [UIImage imageWithContentsOfFile: path];

 

 

 

There are many ways to load images using UIImage. The most common methods are as follows:

1. Use the imageNamed Function

[UIImage imageNamed: ImageName];

2. load data using NSData, for example:

1. NSString * filePath = [[NSBundle mainBundle] pathForResource: fileName ofType: extension];
2. NSData * image = [NSData dataWithContentsOfFile: filePath];
3. [UIImage imageWithData: image];

Because the first method requires less code, many people may use imageNamed to load images. In fact, these two loading methods have their own characteristics.

1) When imageNamed is used for loading, the system caches the image to the memory. If the image is large or there are many images, this method will consume a lot of memory, and releasing the image memory is relatively troublesome. For example, if you use imageNamed to load an image to a dynamic NSMutableArray array, and then assign the array to the animationImages of a UIView object for frame-by-frame animation, this may cause memory leakage. In addition, releasing the memory occupied by images is not that easy. However, using imageNamed to load images also has its own advantages. The same image system only caches it To the memory once, which is very advantageous for reuse of images. For example, if you need to reload the same icon in a TableView, you can use imageNamed to load the image. The system will Cache the icon to the memory. each time you use the image in the Table, only the image pointer is directed to the same memory. In this case, using imageNamed to load images becomes very effective.

2) When the NSData method is used for loading, the image will be loaded to the program by the system as data. When you do not need to reuse the image, or you need to store the image in the database as data, or you want to download a large image over the Internet, use imageWithData to load images as much as possible.

No matter which method is used to load the image, remember to display and release the memory after the image is used.

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.