25 Tips and tricks for enhancing the performance of iOS Apps (Advanced) (1) 2013-04-16 14:56 broken ship House beyondvincentfont Size:T | T
When developing an iOS application, it is critical that your program has good performance. This is also what users expect, if your program runs slow or slow, will incur the user's bad comment. However, due to the limitations of iOS devices, sometimes you want to get good
AD: Network + Offline Salon | Mobile app Mode innovation: give you a reason to do the app >>
When developing an iOS application, it is critical that your program has good performance. This is also what users expect, if your program runs slow or slow, will incur the user's bad comment. However, due to the limitations of iOS devices, it is sometimes difficult to get good performance. There are a lot of things to keep in mind during the development process, and it's easy to forget about the performance impact. This article collects 25 tips and tricks that can improve program performance. Divided into 3 different levels: Beginner, Intermediate, and advanced:
Senior
Use them when and only if the following techniques are able to solve the problem:
22. Speed up startup time
23. Using Autorelease Pool
24. Cache pictures-or do not cache
25. Try to avoid date formatting
Advanced Performance Improvements
Looking for some clever ways to turn yourself into a full code ninja? The following advanced performance optimization techniques allow the program to run as efficiently as possible at the right time!
22) accelerated start-up time
It is important to start the program quickly, especially when the user starts the program for the first time. The first image is very important to the program!
The way to get the program started as quickly as possible is to perform tasks asynchronously, such as network requests, data access, or parsing.
Also, avoid using bloated xibs, because Xib is loaded in the main thread. But remember that storyboard has no such problem-so use storyboard if you can!
Note: When debugging with Xcode, the watchdog does not run and does not connect the device to Xcode when the test program starts performance on the device.
23) Using Autorelease Pool
The NSAutoreleasePool is responsible for releasing an automatically freed object from a block of code. are generally created by Uikit. However, there are cases where you need to create nsautoreleasepool manually.
For example, if you create a large number of temporary objects in your code, you will notice that memory usage is increasing until those objects are freed. The problem is that these objects are freed only when Uikit runs out of autorelease pool, meaning that these objects still occupy resources in memory when these objects are no longer needed.
However, this problem is completely avoidable: Create a temporary object in the @autoreleasepool code block, as follows:
- Nsarray *urls = <# An array of file URLs #>;
- For (Nsurl *url in URLs) {
- @autoreleasepool {
- Nserror *error;
- NSString *filecontents = [NSString stringwithcontentsofurl:url
- Encoding:nsutf8stringencoding error:&error];
- / * Process The string, creating and autoreleasing more objects. * /
- }
- }
When each iteration is completed, all Autorelease objects are released.
For more on NSAutoreleasePool, you can read Apple's official documentation.
24) Cache Pictures--or do not cache
There are two ways to load UIImage from program bundles in iOS.
The first is more common: imagenamed.
The second method is seldom used: imagewithcontentsoffile
Why are there two ways to do the same thing? The advantage of imagenamed is that you can cache images that have already been loaded. Apple's documentation has the following statement:
This method looks in the system caches for a image object with the specified name and returns that object if it exists. If a matching Image object is not already in the cache, this method loads the image data from the specified file, caches I T, and then returns the resulting object.
This method looks for the image in the system cache according to the specified name, and returns if found. If a picture is not found in the cache, the method loads the picture data from the specified file, caches it, and then returns the result.
The Imagewithcontentsoffile method simply loads the picture and does not cache the picture. These two methods are used in the following ways:
- UIImage *img = [UIImage imagenamed:@"MyImage"]; //Caching
- Or
- UIImage *img = [UIImage imagewithcontentsoffile:@"MyImage"]; //No caching
So how do you choose?
If you load a large picture and use it only once, you do not need to cache the image. This is imagewithcontentsoffile appropriate-the system does not waste memory to cache pictures.
However, if you often need to reuse images in your program, it is best to choose the Imagenamed method. This method saves the time it takes to load pictures from disk each time.
25 Tips and tricks for enhancing the performance of iOS Apps (Advanced) (1)