Will the loading of the iOS question load a single picture really not crash?

Source: Internet
Author: User

Today, a buddy goes to a company to interview iOS jobs. One of the topics asked, loading a picture, in the end will not collapse it?

I got this question, then thought is to obtain the network picture, that still may crash, but the actual question, still has half sentence, the picture is local ...

This problem, how does loading local crash? It's not such a problem to write so Giugariu pictures.

===================================================

Originally, the iphone is a handheld device, it occupies a limited amount of memory, when the picture is too large to cause memory-induced crashes.

Later, I checked again, found that there is such a great learning.

Apple's official documentation provides two methods for generating a UIImage object: 1. imagenamed, whose parameter is the name of the picture, and 2. imagewithcontentsoffile, whose parameters are also the path to the picture file. So what's the difference between these two? There must be. According to Apple's Official document: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. So the advantage of imagenamed is that when loading, the picture is cached. So when the picture will be used frequently, then using imagenamed method will be better. For example: You need to load the same icon in the Tableviewcell of a tableview in  , then it is very efficient to load the image 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 the use of 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 detailed usage of two methods is listed below: nsstring *path = [[nsbundle mainbundle] pathforresource:@ "icon"   oftype:@ "PNG"]; Uiimage *image = [uiimage imagewithcontentsoffile:path]; nsstring *filepath  =&nbsP [[Nsbundle mainbundle] pathforresource:filename oftype: "PNG"]; nsdata *image =  [NSData dataWithContentsOfFile:filePath]; UIImage *image = [UIImage  imagewithdata:image]; //or = [uiimage imagewithcontentsoffile:filepath];

The key is

Imagewithcontentsoffile: Loading pictures Only, image data is not cached

and

Imagenamed: The picture is cached in memory before it is displayed.

= = = = = = = = = = = = = = = = = = =============

Later that is the answer to the question, analysis should be sorta, if you have not understand the opinion, you can tell me, ha, progress with each other.



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.