Turn:
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. SoimageNamedThe advantage is that the picture is cached when it is loaded. So when images are used frequently, imageNamed of the Method will be better. For example:you need to load the same icon in a TableView Tableviewcell, then loading the image with imagenamed is highly efficient. 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 useimageNamed caches pictures, puts the image's data in memory, iOS memory is very precious and when memory consumption is too high, it forces the memory to be freed, memory warnings. the memory that releases the image inside the iOS system is a hassle and can cause a memory leak. For example: When a UIView object Animationimages is a dynamic array with UIImage objects nsmutablearray, and the is animated per-frame. When the loads an image into a dynamic array nsmutablearray with imagenamed, 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.
-
NSString*path = [[NSBundleMainbundle]Pathforresource: @ "icon"OfType: @ "PNG"];
-
UIImage *image = [UIImage imagewithcontentsoffile:p Ath];
And:
-
NSString*filepath = [[NSBundleMainbundle]Pathforresource: FileNameOfType: "PNG"];
-
nsdata *image = [nsdata datawithcontentsoffile :filepath];
-
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 loading pictures imagenamed and imagewithcontentsoffile?