IOS performance optimization skills

Source: Internet
Author: User

IOS performance optimization skills
Analyze performance bottlenecks using static Analyze and runtime Profile tools and optimize performance. Based on the problems encountered during development, You can optimize the performance from the following aspects. 1. view optimization 1. Set the opaque View to opaque. 2. reuse, delay loading, or pre-loading View based on actual conditions. 3. Reduce the number of subviews and customize complex Cells Using drawRect. Use drawRect instead of layoutSubView whenever possible. 4. Do not directly call the drawRect and layoutSubviews methods. Alternative methods can be used as a last resort: setNeedsDisplayInRect and layoutIfNeeded. Do not call the alternative methods as much as possible. Solve the problem of re-layout through a reasonable code structure and try to complete the layout at once. Ii. UITableView optimization 1. Correct Use of 'reuseidentifier' to reuse cells. 2. Try to make all view opaque. 3. Reduce the number of subviews and customize complex Cells Using drawRect. 4. Try not to use 'cellforrowatindexpath '. 5. cache as many items as possible, including high rows. Iii. Cache Optimization 1. The cache is unlikely to change but needs to be read frequently. Remote Server Response, images, and computing results. 2. Reuse large-sale objects. For objects with slow initialization, you can add attributes to keep the object only once and reuse it multiple times. For example, NSDataFormatter. 3. cache method pointers. If a method is used in a loop with a large number of cycles, use methodForSelector to obtain the IMP of the method before entering the loop, and directly call the IMP in the loop body. 4. Thread optimization 1. Time-consuming operations are performed by sub-threads or put into the task queue. 2. synchronous use of serial queues instead of synchronization locks. 3. Run unimportant tasks in idle-(void) idleNotificationMethod {// do something here}-(void) registerForIdleNotification {[[nsicationicationcenter defacenter center] addObserver: self selector: @ selector (idleNotificationMethod) name: @ "IdleNotification" object: nil]; NSNotification * notification = [NSNotification notifnotifwithname: @ "IdleNotification" object: nil]; [nsicationicationqueue defaultQueue] Quit Ion: notification postingStyle: NSPostWhenIdle];} 5. Memory Optimization use autorelease pool to reduce the memory peak 6. Code details optimization 1. do not perform time-consuming operations in viewWillApear 2. Use C/C ++ if the key code is more efficient/ C ++ 7. Image optimization decode the image data. After setting the image size in the Child thread, use the scaled image in the imageview. Cause: the imageWithData function of UIImage decompress the Data into an ARGB image every time you draw a picture. Therefore, each time you draw a picture, you can decompress the image, after UIImage initialization, the image is only loaded into the memory, and the actual decoding and re-sampling are performed only when the image needs to be displayed. // Ressample the image, and perform CGSize itemSize = CGSizeMake (width, height) in the Child thread; // the actual size UIGraphicsBeginImageContext (itemSize); CGRect imageRect = CGRectMake (0.0, 0.0, itemSize. width, itemSize. height); [image drawInRect: imageRect]; UIImage newImage = UIGraphicsGetImageFromCurrentImageContext (); // UIGraphicsEndImageContext ();

Related Article

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.