25 Tips and tricks for enhancing the performance of iOS Apps (Advanced) (1)

Source: Internet
Author: User
Tags uikit

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:

  1. Nsarray *urls = <# An array of file URLs #>;
  2. For (Nsurl *url in URLs) {
  3. @autoreleasepool {
  4. Nserror *error;
  5. NSString *filecontents = [NSString stringwithcontentsofurl:url
  6. Encoding:nsutf8stringencoding error:&error];
  7. / * Process The string, creating and autoreleasing more objects. * /
  8. }
  9. }

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:

    1. UIImage *img = [UIImage imagenamed:@"MyImage"]; //Caching
    2. Or
    3. 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)

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.