原文:http://www.raywenderlich.com/31166/25-ios-app-performance-tips-tricks
1. ARC
防止潜在的内存问题
如何使用instrument: http://www.raywenderlich.com/23037/how-to-use-instruments-in-xcode
2. reuseIdentifier
UITableViewCell && UICollectionViewCells && UITableViewheaderFooterViews
3. set opaque to YES
4. avoid fat XIBs
因为XIB在初始化的时候,直接加载所有内容到内存中,开销很大。因此,尽量把XIB拆分开,
只在需要的时候才初始化并加载---这样可以避免一次性加载过于复杂的xib导致内存消耗过大。
5. Don't block the Main Thread
GCD
[NSURLConnection sendAsynchronousRequet: queue: completionHandler:];
6. Size image to imageView' size
因为将一个image在imageView中进行sizeToFit开销很大,所以,对于获取到的image,
最好先resize,再放入到合适的imageView;
7. choose the correct collection
NSArray: ordered list of values, Quick lookup by index, Slow to lookup by value, Slow to insert&delete
NSDictionary: key-value pairs, Quick lookups by key
NSSet: unordered list of values, Quick lookup by value, Quick to insert&delete
8. enable GZIP compression
9. Reuse && Lazy load
仿照UITableView, 不要一次性创建所有的subView, 你只在需要的时候创建一个subView,
并把它添加到reuse queue, 这可以避免大量的消耗。下次使用的时候,你只需要reuse即可。
10. cache
对于不会变化并且经常使用到的object进行cache;对于httpCache,基本的第三方库都已经实现;
对于一般object的NSCache : http://nshipster.com/nscache/
11. Drawing performance
考虑不同绘图机制的性能(UIKit,Core Animation, Core Graphics(高效))
12. 处理Memory Warnings
有3种方式可以接收到Memory Warning
1)applicationDidReceiveMemoryWarning: ---- app delegate
2) didReceiveMemoryWarning ---- UIViewController
3) UIApplicationDidReceiveMemoryWarningNotification ----- notificationCenter
在接收到内存警告的时候,app应该尽可能的释放你能够recreate的资源;
制作你app的过程中,你应当在iOS simulator中模拟内存警告的情况,并作出应对。
13. refuse Expensive Objects
类似于NSDateFormatter, NSCalendar,这些object的初始化,设置效率都比较低,代价比较大.
应对的做法有2种:1)添加为class变量进行reuse
2)创建一个static variable.
对于第二种情况,你应该写一个类似于singleton的方法,来获取这个object.
-(NSDateFormatter*)formatter {
static NSDateFormatter *formatter;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_formatter = [[NSDateFormatter alloc] init];
_formatter.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy"; // twitter date format
});
return formatter;
}
14. Use sprite sheets
15. Avoid Re-Processing Data
这主要是数据处理逻辑规划好
16. choose the right data format
JSON - faster to parse, smaller than XML
XML - soap advantage
17. background image
full size image - [UIImage imageNamed:@"test.png"];
pattern image - [UIColor colorWithPatternImage:[UIImage imageNamed:@"test.png"]];
18. reduce your web Footprint
减少javascript的使用
19. Shadow Path
//Core Animation - 但是这种方式代价高昂
UIView *view = [[UIView alloc]init];
view.layer.shadowOffset = CGSizeMake(-1.0, 1.0);
view.layer.shadowRadius = 5.0;
view.layer.shadowOpacity = 0.6;
//effective way
view.layer.shadowPath = [[UIBezierPath bezierPathWithRect:view.bounds] CGPath];
20. Optimize tableView
reuse cell
set subView opaque
avoid gradients, image scale, offscreen drawing
cache height if height is variable
use asynchronously method for cell' contents
use shadowPath to set shadow
reduce the number of subViews
do as little work as possible in cellForRowAtIndexPath:
use the appropriate data structure
use rowHeight, sectionFooterHeight, sectionHeaderHeight to set constant height instead of delegate(效率上能理解,设计上不能理解)
21. Choose correct data storage option
NSUserDefaults --- small data
XML, JSON, Plist --- expensive operation to store and parse
NSCoding(Archive)---expensive as above
Sqlite---easy to use
Core Data--- like above
22. speed up Launch Time
建议:异步获取数据,避免臃肿的XIB
PS:测试Launch Time的最有效方式:断开Xcode,单独运行你的app
23. load image
imageNamed: 首先从system cache里面查找image,如果没有找到,再从文件加载image
imageWithContentsOfFile: 直接从文件加载image,不检查systemcache
PS:对于会多次使用到的image,选择imageNamed:, 对于单次使用的image,直接从文件加载比较合适
24. Cache Images – Or Not
25. Avoid DateFormatters Where Possible