IOS Load Picture Select Imagenamed method or Imagewithcontentsoffile?

Source: Internet
Author: User

 

Apple's official documentation provides two ways to generate a UIImage object:

1. imagenamed, whose parameters are the name of the picture;

2. Imagewithcontentsoffile, the parameter is also the path of the picture file.

So what's the difference between these two?

There must be. According to Apple's official documentation:

imageNamed: This method finds and returns a picture object in the system cache with a specified name if it exists. If the corresponding picture is not found in the cache, this method loads from the specified document and then caches and returns the object. imageNamedThe advantage is that the picture is cached when it is loaded. So when the image is used frequently, then imageNamed the method will be better. For example: You need to load the same icon in a TableView Tableviewcell, so it is very efficient to load images with imagenamed. The system will cache that icon into memory, and each time the image is used in the Tableviewcell, it will only point the picture pointer to the same piece of memory. 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.

imageWithContentsOfFile: Only pictures are loaded and image data is not cached. 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:

[OBJC]View PlainCopy 
    1. NSString *path = [[NSBundle Mainbundle] pathforresource:@ "icon" oftype:@ "png"];
    2. UIImage *image = [UIImage Imagewithcontentsoffile:path];

And:

[OBJC]View PlainCopy 
    1. NSString *filepath = [[NSBundle Mainbundle] pathforresource:filename ofType: "PNG"];
    2. NSData *image = [NSData Datawithcontentsoffile:filepath];
    3. UIImage *image = [UIImage imagewithdata:image];  //or = [UIImage Imagewithcontentsoffile:filepath];

Again, there are advantages to each of the two usages, and they need to be used for specific application scenarios.

IOS Load Picture Select Imagenamed method or Imagewithcontentsoffile?

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.