原文: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

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

25 iOS performance TipsTricks 笔记相关推荐

  1. IOS开发学习笔记-----UILabel 详解

    IOS开发学习笔记-----UILabel 详解 01 //创建uilabel 02 UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMa ...

  2. ios开发学习笔记--Core Motion

    iOS开发学习笔记之CoreMotion-运动传感器 官网文档:CoreMotion Framework Reference 一.     简介 现在的苹果手机都基本有运动传感器,能够过获取到设备的加 ...

  3. IOS之学习笔记十五(协议和委托的使用)

    1.协议和委托的使用 1).协议可以看下我的这篇博客 IOS之学习笔记十四(协议的定义和实现) https://blog.csdn.net/u011068702/article/details/809 ...

  4. 老李分享:《Java Performance》笔记1——性能分析基础

    老李分享:<Java Performance>笔记1--性能分析基础 1.性能分析两种方法: (1).自顶向下: 应用开发人员通过着眼于软件栈顶层的应用,从上往下寻找性能优化的机会. (2 ...

  5. iOS工程开发笔记二

    iOS工程开发笔记<二> 在Xcode 4, 5的模板工程中可以看到Precompile Prefix Header,但是在Xcode 6被去除了. Xcode 6去掉Precompile ...

  6. IOS开发学习笔记(一)

    概述: iOS是苹果开发的手持设备操作系统(iPhone,iPad,iPod touch,iPad mini). 基于UNIX,层次架构:核心操作系统层(Core OS)-> 核心服务层(Cor ...

  7. ios逆向入门笔记-HOOK-QQ登录

    ios逆向入门笔记-HOOK-QQ登录 选择目标 砸壳 基本调试 Reveal+确定目标action及target 利用Logos 进行 hook 后续 选择目标 在经过之前的配置,这次有点小改动= ...

  8. 开源高仿印象笔记的iOS应用——朝夕笔记(Oncenote)

    iOS-Oncenote    这是一款类似于印象笔记Evernote的生活类iOS应用--朝夕笔记 Oncenote.我希望能为更多的iOS开发者提供帮助与服务.当然App中还有不少bug和可扩展的 ...

  9. iOS 疑难杂症 学习笔记

    iOS Learing note 出现问题:虚拟机 Xcode 调试时候发现键盘出不来 错误提示:Xcode 提示: Can't find keyplane that supports type 8 ...

最新文章

  1. HDU5762(暴力)
  2. PostgreSQL9.6+新增空闲事务自动查杀功能
  3. 【BZOJ】4873: [Shoi2017]寿司餐厅
  4. 【flask整合深度学习】ubuntu系统下显示深度学习视觉检测结果图片并可在web端访问,配置允许手机浏览器打开
  5. 无极绳连续牵引绞车_可视化无极绳绞车保护控制装置
  6. 【Alpha】开发日志Day8-0719
  7. 龙腾世纪:起源(推荐一个可以用来英语学习的RPG游戏)
  8. 谷歌浏览器无法登陆_论坛上传图片后自动退出登陆?你不是一个人,原因及解决方法来了...
  9. OpenCV4机器学习(一):OpenCV4+VS2017环境搭建与配置
  10. 游戏开发——Gris日志 day01
  11. 利用Crimestat(犯罪统计)软件进行空间分析
  12. cf3月活动大全 3月活动汇总2015
  13. NDK开发——FFmpeg实现视频转YUV、视频转RGB显示、音频转PCM、音频播放、音视频同步
  14. 【数据挖掘】鸢尾花分析实验与数据降维
  15. GBA程序开发入门1
  16. 锁定计算机重启会变更ip吗,计算机更改IP不需要重启的方法
  17. 如何成为用户真正需要的短信验证平台
  18. Apple Mac OS 0.97太酷了。。。呵呵,顺便分享某版本Mac源码
  19. 深度神经网络调参-学习笔记
  20. TALIB 中文文档 Overlap Studies Functions 重叠研究指标

热门文章

  1. 企业级java springcloud b2bc商城系统开源源码二次开发-负载均衡策略...
  2. Java多线程学习笔记之二缓存
  3. 如何估算文章阅读时长?
  4. 病毒木马查杀实战第019篇:病毒特征码查杀之编程实现
  5. mvn 打包项目到eclipse
  6. JS原始类型:数值的运用技巧
  7. IE6下绝对定位的DIV被相对定位的DIV挡住的问题
  8. Permutation Sequence
  9. windows Server 2003   IIS启用父路径
  10. Service Bus EAI EDI 实验室发布了