iOS性能优化三部曲(终极版)

iOS性能优化是一个比较头痛的问题,经过前面两篇文章的简要介绍估计大家已经有了了解,那么我们就向大牛进发吧!!!
1.加速启动时间
用户由于不懂技术,当我们的app的数据如果不能及时显示数据肯定会引起用户的吐槽,我们尽可能做的是使用恰当的异步任务加载远端、数据库数据和解析数据。
避免过多使用XIB,因为XIB的加载是在主线程中加载的,我们还是让主线程全心全意的负责UI吧!!

2.使用Autorelease Pool
NSAutoreleasePool负责释放放入到释放池中的对象,随着对象的越来越多,内存会被占用的越来越多,这是系统会将释放池中长久不用的对象进行释放
当我们要操作非常非常多的对象时,我们可以自己创建释放池,将对象慢慢地被释放,如下:

    NSArray *urls = An array of file urls;
    for (NSURL *url in urls) {
        @autoreleasepool {
            NSError *error;
            NSString *fileContents = [NSString stringWithContentsOfURL:url usedEncoding:NSUTF8StringEncoding error:&error];
        }
    }

这段代码在每次遍历后释放所有autorelease对象

3.选择是否缓存图片
常见的从缓存中加载图片有两种方式,一个:是用 imageNamed:,第二个是:imageWithContentsOfFile:,第一种方法比较常见,既然两种方法都是加载图片,那两者之间有什么区别那?
imageNamed加载图片时会缓存图片。apple文档有这样一段描述:这个方法在用指定的名字在系统缓存中查找并返回一个图片对象如果存在的话,如果在缓存中没有找到相应的图片,这个方法会从指定的文档中加载然后缓存并返回对象,而imageWithContentsOfFile:方法仅仅加载图片,面对这种情况下我们如何选择?
如果你要加载一个大图并且一次性使用的话,我们就没必要缓存这个图片了,用imageWithContentsOfFile:即可,没必要浪费内存来缓存它了!相反,如果图片多次使用,我们最好使用imageNamed:,缓存图片以防以后使用

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS and Macos Performance Tuning: Cocoa, Cocoa Touch, Objective-C, and Swift (Developer's Library) by Marcel Weiher English | 3 Mar. 2017 | ISBN: 0321842847 | 400 Pages | EPUB/PDF (conv) | 42.64 MB In iOS and macOS™ Performance Tuning, Marcel Weiher drills down to the code level to help you systematically optimize CPU, memory, I/O, graphics, and program responsiveness in any Objective-C, Cocoa, or CocoaTouch program. This up-to-date guide focuses entirely on performance optimization for macOS and iOS. Drawing on 25 years of experience optimizing Apple device software, Weiher identifies concrete performance problems that can be discovered empirically via measurement. Then, based on a deep understanding of fundamental principles, he presents specific techniques for solving them. Weiher presents insights you won’t find anywhere else, most of them applying to both macOS and iOS development. Throughout, he reveals common pitfalls and misconceptions about Apple device performance, explains the realities, and helps you reflect those realities in code that performs beautifully. Understand optimization principles, measurement, tools, pitfalls, and techniques Recognize when to carefully optimize, and when it isn’t worth your time Balance performance and encapsulation to create efficient object representations, communication, data access, and computation Avoid mistakes that slow down Objective-C programs and hinder later optimization Fix leaks and other problems with memory and resource management Address I/O issues associated with drives, networking, serialization, and SQLite Code graphics and UIs that don’t overwhelm limited iOS device resources Learn what all developers need to know about Swift performance

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值