[UIImage imagenamed:] Only suitable for reading with the map in the UI interface, larger resource files should be avoided as far as possible
The most common way to load local images with UIImage is the following three:
1. Using the Imagenamed method
[UIImage Imagenamed:imagename];
2. Using the Imagewithcontentsoffile method
NSString *thumbnailfile = [NSString stringwithformat:@ "%@/%@.png", [[NSBundle Mainbundle] resourcePath], fileName]; UIImage *thumbnail = [UIImage imagewithcontentsoffile:thumbnailfile];
3. Using the Initwithcontentsfile method
UIImage *image = [[UIImage alloc] Initwithcontentsoffile:filepath]
The first method is a common method, which makes it easy to load resource images. When loaded in imagenamed, the image data is cached in system memory according to its name to improve the performance of the Imagenamed method to obtain the image object of the same image. This cache is not freed even if the generated object is Autoreleasepool released. and there is no clear method of release. If the image is larger, or the image is more, it consumes a lot of memory in this way.
The second method of loading the picture is not cached. The resulting object is autorelease when released when Autoreleasepool is released.
The third method is to manually release the drop. No system cache. Released immediately after release, generally used in the cover and other pictures of the larger place.
When loaded in a imagenamed manner, the system caches the image to memory. If the image is larger, or the image is more, it consumes a lot of memory in this way, and releasing the memory of the image is a relatively troublesome thing. For example, if you use imagenamed to load an image into a dynamic array nsmutablearray and then animate the array to a animationimages of an UIView object, then this is likely to cause a memory leak. And the memory that is occupied by releasing the image is not that simple. But using imagenamed to load images also has its own advantages. The same image system will only cache it to memory once, which is very advantageous for the reuse of images. For example: You need to reload the same icon in a tableview, then load the image with Imagenamed, the system will cache the icon to memory, in the table every time the image is used, only the picture pointer to the same piece of memory. In this case, loading the image using imagenamed becomes very effective.
There are 3 ways to load images commonly used by uiimage: imagenamed, Imagewithcontentsoffile, Initwithcontentsfile.
Imagenamed:
UIImage image = [UIImage imagenamed:@ "Image.gif"]. The object to be obtained is autorelease. This method is a bit special, it will be generated image object at the same time, the images of the data according to its name in the system memory, to improve the Imagenamed method to obtain the same image of the image object performance. This cache is not freed even if the generated object is Autoreleasepool released. This is useful when you have a large number of identical images in your app, which can improve performance and memory utilization.
Imagewithcontentsoffile:
UIimage image = [UIimage imagewithcontentsoffile:@ "path"]. The resulting object is autorelease when released when Autoreleasepool is released. No system cache.
Initwithcontentsfile
UIimage image = [[UIimage alloc] init initwithcontentsfile]. To the object is useless, you want to manually release off. No system cache. Released immediately after release, generally used in the cover and other pictures of the larger place.
Using the Imagenamed method, using the same picture to paste multiple imageview should be greatly optimized, time consuming and memory is very small, and the use of imagewithcontentsoffile has a huge consumption:
The generated UIImage object memory address generates 100,000 identical filenames using the same file name as 185
The UIImage object's inner Uiimageview object
Store and time-consuming stickers
Memory: 28.70m->32.90m Memory: 29.69m-32.84m
imagenamed mode the same memory address takes time: instantaneous time: instantaneous
Imagewith Memory: 29.38m->300.96m Memory: 30.21m->537.57m
Contentsoffile different memory address time: 30 seconds Time: 40 seconds or more
There are many ways to load images with uiimage, the most commonly used are the following two kinds:
1. Using imagenamed function
[UIImage Imagenamed:imagename];
2, Loaded in NSData way, for example:
1. NSString *filepath = [[NSBundle mainbundle] Pathforresource:filename oftype:extension];
2. NSData *image = [NSData Datawithcontentsoffile:filepath];
3. [UIImage Imagewithdata:image];
Because the first way to write less code, it is possible to compare multiple people to use imagenamed to load the image. In fact, both of these loading methods have their own characteristics.
1) when loading in imagenamed mode, the system caches the image to memory. If the image is larger, or the image is more, it consumes a lot of memory in this way, and releasing the memory of the image is a relatively troublesome thing. For example, if you use imagenamed to load an image into a dynamic array nsmutablearray and then animate the array to a animationimages of an UIView object, then this is likely to cause a memory leak. And the memory that is occupied by releasing the image is not that simple. But using imagenamed to load images also has its own advantages. The same image system will only cache it to memory once, which is very advantageous for the reuse of images. For example: You need to reload the same icon in a tableview, then load the image with Imagenamed, the system will cache the icon to memory, in the table every time the image is used, only the picture pointer to the same piece of memory. In this case, loading the image using imagenamed becomes very effective.
2) when loading with NSData mode, the image is loaded into the program by the system in data mode. When you don't need to reuse the image, or if you need to store the image as data in a database, or if you want to download a large image over the network, try to load the image using Imagewithdata.
No matter which way the image is loaded, after the image is used, be sure to remember to show free memory.
Imagenamed, Imagewithcontentsoffile, Initwithcontentsfile difference