博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
25 iOS performance Tips&Tricks 笔记
阅读量:6869 次
发布时间:2019-06-26

本文共 3514 字,大约阅读时间需要 11 分钟。

原文

 

 

1. ARC

    防止潜在的内存问题

   如何使用instrument: 

 

 

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

 

 

11. Drawing performance

    考虑不同绘图机制的性能UIKitCore AnimationCore 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.

-(*)formatter {

    static *formatter;

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        _formatter = [[ 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

 

 

 

 

转载于:https://www.cnblogs.com/traximus/p/3316399.html

你可能感兴趣的文章
python实现linux下指定目录下文件中的单词个数统计
查看>>
SQL SERVER存储过程中如何使用事务与try catch
查看>>
没什么不可能:剿灭Windows下的29个烦恼
查看>>
String,StringBuffer,StringBuilder的整理
查看>>
mysql 字符截取 实列
查看>>
部署mysql高可用、读写分离集群
查看>>
jquery中下拉多选插件jquery.multiSelect的使用
查看>>
梦想与现实,你会选择什么——一个电子爱好者的迷茫
查看>>
用rabbitMQ实现生产者消费者
查看>>
GRADLE遇见“设备未就绪”
查看>>
正则表达式实现——匹配括号中的A 以及 匹配非括号中的A
查看>>
golang锁sync.Mutex
查看>>
定义自己的JSTL标签库
查看>>
JAVA 相关
查看>>
PowerDesigner导出数据结构到word
查看>>
性能调优--永远超乎想象
查看>>
zabbix监控mysql
查看>>
cisco路由器上做端口映射
查看>>
Lua 和 C/C++ 互相调用实例分析
查看>>
初见:存储过程 调用方法
查看>>