25 Tips and tricks for enhancing the performance of iOS Apps (intermediate) (2) 2013-04-16 14:42 broken ship House beyondvincentfont Size:T | T
This article collects 25 tips and tricks for improving program performance into 3 different levels: Beginner, intermediate, and advanced. You can also click to view the introductory article.
AD: Network + Offline Salon | Mobile app Mode innovation: give you a reason to do the app >>
13) Reuse of expensive objects
Some objects are very slow to initialize-such as NSDateFormatter and Nscalendar. However, you can sometimes avoid using these objects, such as when parsing dates in Json/xml.
When using these objects, to avoid performance bottlenecks, try to reuse the objects as much as possible-adding a property to a class or creating a static variable.
Note that if you use a static variable, the object will persist when the program is running, just like a single case.
The following code demonstrates creating a deferred-loading date format property. The first time a property is called, a new date format is created. After that, the instance object that has been created is returned:
- In your. h or inside a class extension
- @property (nonatomic, strong) NSDateFormatter *formatter;
- Inside the implementation (. m)
- When your need, just use Self.formatter
- -(NSDateFormatter *) Formatter {
- if (! _formatter) {
- _formatter = [[NSDateFormatter alloc] init];
- _formatter.dateformat = @"EEE MMM dd HH:mm:ss Z yyyy"; //Twitter date format
- }
- return _formatter;
- }
Also, keep in mind that when you set the date format for NSDateFormatter, it's as slow as creating a new NSDateFormatter instance Object! Therefore, it is very good to reuse the NSDateFormatter if the date format needs to be processed frequently in the program.
14) using Sprite Sheets
Using Sprite Sheets
Are you a game developer? Yes, then the sprite sheets is one of the best choices. Using sprite sheets is quicker to draw and consumes less memory than the usual drawing methods.
Here are two very good sprite sheets tutorials:
How to use animations and sprites in cocos2d Sheets
How to use texture packs (Texture Packer) and pixel formats in cocos2d to create and optimize sprite Sheets. (The second tutorial provides a detailed description of the pixel format-a measure of performance impact in a game)
If you are not familiar with sprite sheets, you can look at the introduction: spritesheets– video, Part 1 and Part 2. The author of these two videos is Andreas Löw, the creator of the texture Pack (Texture Packer), which is an important tool for creating a sprite sheets.
In addition to using sprite sheets, here are some tips for game development, for example, if you have a lot of sprites (like shooting games), you can reuse sprites instead of creating sprites every time.
15) Avoid data re-processing
Many programs need to get data from a remote server to meet the needs of the program. This data is typically in JSON or XML format. It is important to use the same data structure when requesting and receiving data.
Why is it? converting data into a program-appropriate data format in memory is an additional cost.
For example, if you need to display some data in a table view, the data format that is requested and received is preferably in array format, which avoids some intermediate operations-converting the data to a data structure that is appropriate for the application.
Similarly, if a program is accessing a specific value based on a key, it is best to request and receive a dictionary of key/value pairs.
The data obtained at the first time is the desired format and avoids the additional cost of converting the data into a program-friendly data format.
16) Select the correct data format
Select the correct data format
There are several ways to upload data from a program to a network server, where the data format used is basically JSON and XML. All you need to do is choose the correct data format in the program.
JSON is very fast to parse, and is much smaller than XML, which means that you only need to transfer less data. And after IOS5, there is already a built-in JSON deserialization API, so it's easy to use JSON.
However, XML has its own advantages: if you use the Sax method to parse XML, you can read the XML edge parsing, and do not wait until all the XML gets to start parsing, which is different from JSON. This approach improves performance and reduces memory consumption when processing large amounts of data.
17) Set the appropriate background image
In iOS encoding, like many other things, there are two ways to set a background image for a view:
1. You can use the Uicolor Colorwithpatternimge method to create a color and set the color to the background color of the view.
2. You can add a Uiimageview child view to the view.
If you have a full-size background picture, you should use Uiimageview, because Uicolor's Colorwithpatternimge method is used to create a small picture-the image is reused. Using Uiimageview at this time can save a lot of memory.
- You could also achieve the same result in Interface Builder
- Uiimageview *backgroundview = [[Uiimageview alloc] initwithimage:[uiimage imagenamed:@"background"];
- [Self.view Addsubview:backgroundview];
However, if you plan to use small images as backgrounds, you should use the Uicolor Colorwithpatternimge method. In this case, the drawing will be fast and will not consume a lot of memory.
- Self.view.backgroundColor = [Uicolor colorwithpatternimage:[uiimage imagenamed:@"background"];
25 tips and tricks for enhancing the performance of iOS Apps (intermediate) (2)