iOS性能优化

398 阅读3分钟

##程序方面优化

  • 静态分析
    1. Xcode静态分析Proudct-Analyze
    2. 自动静态分型Build Settings中Analyze During 'Build'设置为YSE开启
  • 动态检测 工具MLeaksFinder和Xcode自带工具Instruments的Leaks Allocation面板显示了“已被创造并且仍然在内存中”,哪个对象是最占内存的。收到一级内存警告使用此工具查看内存使用情况。细节可以显示哪个类,哪行代码来创建,使用,处理这些对象。出现EXEC_BAD_ACCESS可以使用Allocation工具定位具体crash代码。
  • 析构
    每个对象些分类对象写delloc方法

##性能检测工具

  • CPU使用情况
    最坏的情况是CPU有太多的任务需要处理(比如用户输入、读写文件、网络交互),CPU不应该有空闲时间。必须使用多线程技术充分利用CPU。
    测试工具:Instrument的Sampler面板 这个工具可以对应用以固定的频率进行取样;默认的频率是10ms,但 是你可以更改这个值。当CPU每次被取样的时候,这个工具就对其进行⼀一 次记录。这些记录和样本通常足够你来辨识性能瓶颈所在,并且对其进行 修复了。

  • 电池损耗 Run-Profile-Instrument打开Energy Diagnostics

  • CoreAnimation
    检测的内容就是你的 UI 线程在每秒可以渲染多少帧。UI 线 程也是你在检测用户交互方面所要考虑的主要线程;只能在真机上测试。问题代码在哪不能个确定

  • Color Blended Layer 把 你的视图设置为不透明的,可以让渲染工作的速度变得更快,因为GPU不用在同 一个点上进行二次渲染.
    不透明:绿色 透明:红色

  • 文件和网络连接测试工具 Directory I/O 和 File Activity ##复用 ###图像复用 复用Cell,复用图片 [UIImage imageNamed:@""],imageNamed 方法做 了一项很重要的工作:它将所加载的图像在内存中进行了缓存,当你再次调用这 个图像的时候,就能直接从内存进行复用了。它只能加载那些在源代码中就有的图像。
    [[UIImage alloc] initWithContentsOfFile:@""]或者[[UIImage alloc] initWithData:Data]操作系统通常不会在内存中自动地进行缓存操作。

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *CellIdentifier = @"CellIdentifier"; 
	ReuseTableViewCell *cell = (ReuseTableViewCell *) [self getCellWithTableView:tableView cellIdentifier:CellIdentifier nibName:@"ReuseTableViewCell"];
	NSString *avatarFile = [NSString stringWithFormat:@"a0"];
	NSString *avatarName = [[NSBundle mainBundle] pathForResource:avatarFile ofType:@"jpeg"]; cell.avatar.image = [self imageWithName:avatarName];
	cell.userName.text = [NSString stringWithFormat:@"hi here: %d", indexPath.row]; // Configure the cell. return cell;
}

// 从字典里获取图片
- (UIImage *)imageWithName:(NSString *)name {
	if ([self.imageDictionary objectForKey:name]) { 
		return [self.imageDictionary objectForKey:name]; 
	}
	
	UIImage *image = [[UIImage alloc] initWithContentsOfFile:name]; [self.imageDictionary setObject:image forKey:name];
	return image;
}

##减少准备的时间 如果你在此等待了过长的时间,那么 UI 的渲染过程就会被阻塞;应用既 不能显示新的内容,也不能做其他的工作。这就是为什么用户会看到滚动永远的 停滞在一个地方的原因之一。

	- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // Initialize and return the Cell here
}