Analyze performance bottlenecks and optimize performance with the static Analyze tool, as well as the run-time profile tool. Combined with my problems encountered in the development, can be from the following aspects of performance optimization.
First, view optimization
1. The opaque view is set to opaque.
2, according to the actual situation reuse, delay loading or preload view.
3, reduce the number of subviews, custom complex cell use DrawRect. Try to use DrawRect instead of Layoutsubview.
4, not directly call DrawRect, Layoutsubviews method. As a last resort, you can use alternative methods: setneedsdisplayinrect,layoutifneeded, alternative methods also try not to invoke, through a reasonable code structure to solve the problem of re-layout, as far as possible to complete the layout.
Second, UITableView optimization
1. Correct use of ' reuseidentifier ' to reuse cell.
2, try to make all the view opaque.
3, reduce the number of subviews, custom complex cell use DrawRect.
4, try not to use ' Cellforrowatindexpath '.
5. Cache as many things as possible, including row height.
Third, cache optimization
1, the cache is unlikely to change but need to read something frequently. The response, picture, and calculation results of the remote server.
2. Reuse large overhead objects. For objects that are slow to initialize, the object is persisted by adding a property, guaranteed to be initialized only once, and reused multiple times. such as Nsdataformatter.
3, method pointer cache. If a method is used in a loop with very many cycles, the IMP is called directly in the loop body by using Methodforselector to obtain the imp of the method before entering the loop.
Four, thread optimization
1, time-consuming operations are performed using child threads, or placed in the task queue.
2, synchronous use of serial queues instead of synchronous locks.
3, non-critical tasks run in idle
- (void)idleNotificationMethod { // do something here}- (void)registerForIdleNotification { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(idleNotificationMethod) name:@"IdleNotification" object:nil]; NSNotification *notification = [NSNotification notificationWithName:@"IdleNotification" object:nil]; [[NSNotificationQueue defaultQueue] enqueueNotification:notification postingStyle:NSPostWhenIdle];}
Five, memory optimization
Use Autorelease pool to reduce memory spikes
Six, Code details optimization
1, not in the viewwillapear time-consuming operation
2. If the key code is more efficient with C/C + +
Seven, picture optimization
Decode the image data. After you set the size of the image in a child thread, the scaled image is used in ImageView. Reason: Because UIImage's Imagewithdata function is to extract data into the ARGB image each time the drawing, so in each drawing, there will be an decompression operation, UIImage initialization is simply to load the image into memory, The actual decoding and resampling is performed only when the image needs to be displayed.
//图片重采样,在子线程中进行CGSize itemSize = CGSizeMake(width, height);//实际要缩放的大小UIGraphicsBeginImageContext(itemSize);CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);[image drawInRect:imageRect];UIImage newImage = UIGraphicsGetImageFromCurrentImageContext(); //重采样后的图片UIGraphicsEndImageContext();
iOS Performance Tuning Tips